Hi, I observed a strange behaviour (for me only) of void pointer. I wrote a code to access an integer through void pointer as below:
void *ptr; int a[2] = {1,2};
ptr = (int *)&a; ptr++; // This is giving error: Unknown Size
Why the above increment statement is giving error as i already cast the pointer to int type ?
Can anybody help me in understanding this ?
Karthik K.
Thats absolutely true ..!!
So the above statement gets the address of an address.
That interpretation would be consistent with the way you'd expect operator & to work, but that behavior doesn't actually match the C spec.
Array names are not always the same as pointers to the first element. Most of the time, yes, but not always.
When applied to an array, the & operator does nothing.
char c[100];
char* p = c; char* q = &c;
assert (p == q); // this assertion is true
(This makes some sense if you ask yourself what &c means. It's the address of the array c, which is the same as the address of its first element. Also, since c is really a constant determined no later than load time, it doesn't really have an address to take. It's not explicitly stored anywhere.)
Similarly, sizeof behaves a little oddly around arrays.
assert (sizeof(c) == 100 * sizeof(char)); assert (sizeof(c) != sizeof(char*));
But, this latter point is only true when sizeof can "see" the array definition. If c were a parameter to a function, then sizeof(c) would indeed be the size of a pointer, no matter what the array declaration was.
"((int *)ptr)++;
warning: #1441-D: nonstandard cast on lvalue
Is that ok?"
Although some compilers accept it, it's really an error because the postfix increment operator requires a modifiable lvalue and a cast does not yield an lvalue.
What does work on all compilers is:
ptr = ((int *)ptr) + 1;
is there a reason to learn C, and THEn apply to ucontrollers, certainly there should be other ways,
There are as many 'ways' as there are peaches in Gaffney, but "to learn C, and THEn apply to ucontrollers" is the best and - surprise - fastest.
Erik
andy, do u take classes for C? got to start from 'c'ing c..
ANDY, that was a nice answer, applauds, is there a reason to learn C, and THEn apply to ucontrollers,
certainly there should be other ways,
Hi,
independend from the void or int pointer type, the statement "ptr = (int *)&a;" is wrong.
If a array is defined, the name of the array is the address. So the above statement gets the address of an address.
Correct is either: ptr = (int *)a; or ptr = (int *)&a[0];
Thanks Neil for your suggestion. I know the basic of C, but some miscellaneous concept like void pointer, null pointer, dangling pointer,etc.. will not be explored in most of the book. Generally C book gives the basic programming guide, not the in depth knowledge. Or may be but i am not aware of such book.
If any one have idea then please let me know.
Thanks Neil for your suggestion. I know the basic of C, but some miscellaneous concept like void pointer, null pointer, dangling pointer,etc.. will be explored in most of the book. Generally C book gives the basic programming guide, not the in depth knowledge. Or may be but i am not aware of such book.
"I want to improve my C knowledge. Can anyone suggest a way... ?"
Do a 'C' programming course.
Get a decent 'C' textbook.
Probably best to learn the language on a PC or similar; then, once you know the language, you can learn how to apply it to microntrollers...
ya ok,
get it done by somebody;
((int *)ptr)++;
Is that ok?
Thanks to all for guiding me... So if i understood correctly, then i can say that whenever i need to access void pointer, i have to cast it to proper type.
Well, I want to improve my C knowledge. Can anyone suggest a way... ?
Jon, I was trying to encourage a good style of programming in C, and you ruined it by showing an easy workaround :-) Ah well, let him learn the hard way.
View all questions in Keil forum