• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

errors handling pointers.

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int* read(){
int i,num,array[11];
printf("enter a integer value between 1 & 10: ");
scanf("%i",&num);
while(!(num>0&&num<11)){
read() ;
}

if(num>0&&num<11){
printf("enter %i integers now: ",num);
for(i=0;i<num;i++)
scanf("%i",*array++);//LINE 12

*array=0;
}
return array;
}
why it shows following compile errors when i try to invoke this function, can you please explain me? erros:[Error] C:\Users\seeker-PC\Documents\C-Free\Projects\n\Untitled12.cpp:19: error: ISO C++ forbids cast to non-reference type used as lvalue
[Error] C:\Users\seeker-PC\Documents\C-Free\Projects\n\Untitled12.cpp:19: error: non-lvalue in assignment
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In C and C++, an lvalue is anything that can be assigned to; its name comes from anything that can be on the left-hand side of an assignment operator.



Other assignments can involve lvalues implicitly, even though no assignment operator is involved. the ++ operator, for example, assigns a new value to its operand. But the same rules apply:



Your program is violating these rules, hence the error message you get. If we look at the line that's the problem:



we realize that you want to pass a pointer to an integer in the array to scanf() so that scanf() has a place to put the integer it read. Then, you want to increment the pointer so the next time through the loop you have a new place for the next integer.

Problem is, you can't change the "array" variable like that; it's not an lvalue. The way to make your code work would be to get another pointer -- since pointers are lvalues and arrays are not -- and use that, instead. If you initialize that pointer to the first element in the array, you can walk along that array as you expect:



Your function has another serious problem, though: it's returning a pointer to memory that's local to the stack. You'll want to fix that issue, too.

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic