File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes C / C++ and the fly likes errors handling pointers. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Languages » C / C++
Bookmark "errors handling pointers." Watch "errors handling pointers." New topic
Author

errors handling pointers.

Nilushika Udayangani
Greenhorn

Joined: Jun 22, 2013
Posts: 8
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
Mike Blaszczak
Greenhorn

Joined: Sep 02, 2013
Posts: 22
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.

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: errors handling pointers.
 
Similar Threads
not able to call my header file
why does core java doesnt have unsigned int??
Program for trailing zeroes
problem in simple bubble sort logic..
C2143 error and I can't find it