I have this line:
But when it prints out, i receive this:PHP Code:
printf("c[bk]: %c \t c+bk: %c\n",camadas_dados[bk],camadas_dados+bk);
http://xs125.xs.to/xs125/08141/lol647.jpg
Why are they different? How can i make them equal?
Thanks!
I have this line:
But when it prints out, i receive this:PHP Code:
printf("c[bk]: %c \t c+bk: %c\n",camadas_dados[bk],camadas_dados+bk);
http://xs125.xs.to/xs125/08141/lol647.jpg
Why are they different? How can i make them equal?
Thanks!
The subscript operator dereferences the address
is equivalent tocamadas_dados[bk]
Code:*( camadas_dados + bk )
My best code is written with the delete key.
array[0] is the first element of the array. array + 0 is a pointer to the first element of the array. Clearly, a value and a pointer to that value are fundamentally different.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Thanks!
I think it depends on what Milhas actually wants to do, e.g., from the example I would say that camadas_dados + bk should be *(camadas_dados + bk) as in Prelude's example.That is,
camadas_dados[bk]
Should actually be
&camadas_dados[bk]
Also, why use &camadas_dados[bk] instead of camadas_dados + bk? It seems to me that if bk is equal to the number of elements of the array, camadas_dados + bk would result in a one past the end pointer, but &camadas_dados[bk] would take the address of a non-existent object.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Whilst technically there may be a subtle difference, if we are talking char arrays (or any other plain old data/C types), rather than some C++ type where [] is overloaded with a function, &camadas_dados[bk] and camadas_dados + bk would generate exactly the same code [and both produce the address of the element immediately after the array memory provided if bk == number of array elements. The only difference is style.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Oops, yes, you're right in that it wasn't supposed to take the address.
The correct statement would be that
camadas_dados + bk
Is the same as
&camadas_dados[bk]
And
camadas_dados[bk]
Is the same as
*(camadas_dados + bk)
Which one to use is purely up to the programmer.
I Want atoi to do this:
atoi(camadas_dados[bk]);
but it gives me warnings at compilation.
atoi(*(camadas_dados+bk)); gives the same warning
atoi(&camadas_dados[bk]); returns strange addresses.
Edit: And atoi(camadas_dados+bk); returns Zero everytime
The last line is correct. But as to the problem, it can be many things. It's better to show the code so we can do a diagnostic.
(Why is it correct? Because atoi wants a pointer to char [char*], and [] derferences the pointer [or array] [and the same with *], so putting a & [address of operator] there takes the address from that element forwards.)
Assuming that camadas_dados is declared as:
then this form is the correct one.Code:char camadas_dados[X];
If you get strange results from that, perhaps your string isn't correctly terminated, or the string is not a number in some way.Code:atoi(&camadas_dados[bk]); // or atoi(camadas_dados + bk);
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
In practice, I think that is what will happen, but semantically speaking, I thought that &camadas_dados[bk], where bk is equal to the number of elements of camadas_dados, is undefined.&camadas_dados[bk] and camadas_dados + bk would generate exactly the same code [and both produce the address of the element immediately after the array memory provided if bk == number of array elements.
However, the C99 Standard states:
So I am wrong to think that &camadas_dados[bk] would take the address of a non-existent object under the circumstances described.Originally Posted by Section 6.5.3.2
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
If i have char array[3] = "123"
and give atoi(&array[1]), atoi will give me int '2' or int '23' ?