language: C
*s = "3 ..."
expected zoom == 3
result is 13. Why?
image:
http://oi58.tinypic.com/j8ljfa.jpg
same result 13Code:zoom = (char) atoi(*s);
language: C
*s = "3 ..."
expected zoom == 3
result is 13. Why?
image:
http://oi58.tinypic.com/j8ljfa.jpg
same result 13Code:zoom = (char) atoi(*s);
Works for me:
Running the above program gives me the output of: 3Code:#include <stdio.h> #include <stdlib.h> int main(void) { int zoom; const char *x; const char **s = &x; *s = "3 ..."; zoom = atoi(*s); printf("%d\n", zoom); return 0; }
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
You didn't check the image which shows the code and what i see in watch panel while debuging.
You didn't check what type is zoom.
I did some change to the code as I tried to correct the problem:
The problem is that I expect * zoom to point to struct with z member. z meber is char.Code:char * zoom = &(w->task.temp.blocksInfo[w->task.temp.square_no].z); *zoom = (char) atoi(*s);
I expect the zoom should point to the z member and be set 3.
But what I got is zoom: 0x3e28d8 "0x3e28d8 "\003đ..ş\rđ..ş\rđ..ş\rđ..ş\rđ..ş\rđ..ş\rđ..ş\rđ.. ş\rđ..ş\rđ..ş\rđ..ş\rđ..ş\rđ..ş\rđ..ş\rđ..ş\rđ..ş\ rđ..ş\rđ..ş\rđ..ş\rđ..ş\rđ..ş\rđ..ş\rđ..ş\rđ..ş\rđ ..ş\rđ..ş\rđ..ş\rđ..ş\rđ..ş\rđ..ş\253\253\253\253\ 253\253\253\253""
What am I doing wrong?
In the image which I
Edit:
Actually I did not correctly said what I need. I need to set the w->task.temp.blocksInfo[w->task.temp.square_no].z via zoom pointer. Now I see it really works
after the correction which I did (added pointer to zoom). But what does not look correct is the value of zoom. There is array instead single char.
w->task.temp.blocksInfo[w->task.temp.square_no].z : 3 '\003'
zoom : 0x3e28d8 "\003đ..ş\rđ..ş\rđ..ş\rđ..ş\rđ..ş\rđ..ş\rđ..ş\rđ.. ş\rđ..ş\rđ..ş\rđ..ş\rđ..ş\rđ..ş\rđ..ş\rđ..ş\rđ..ş\ rđ..ş\rđ..ş\rđ..ş\rđ..ş\rđ..ş\rđ..ş\rđ..ş\rđ..ş\rđ ..ş\rđ..ş\rđ..ş\rđ..ş\rđ..ş\rđ..ş\253\253\253\253\ 253\253\253\253"
Last edited by barracuda; 04-27-2015 at 08:31 AM.
You did not post the smallest and simplest compilable program that demonstrates the problem.Originally Posted by barracuda
The reason why I wrote what I did was to show you how easy it is to see with a small and simple compilable example what you're doing right or wrong. When you face such problems, this is one of the tools that you can use to help you debug: set aside your current program and try to come up with the smallest and simplest program that you expect should compile or run with the expected result but which fails to compile or runs with an unexpected result.
Anyway, checking the image: your arrow is wrong. On the first line of the variables being watched is zoom, which clearly has a value of 3. Your arrow that points to the assignment to zoom in your code actually should point to the previous line where you assign an address to zoom. This assignment does not make sense since zoom is a char, not a pointer.
In your updated code snippet, you changed zoom to be a pointer to char. This could work, except that this means that you are actually storing an int in a char, but an int does not necessarily fit into a char.
You probably examined or printed zoom instead of *zoom (or equivalently, the z member).Originally Posted by barracuda
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
since zoom is char* the VS tries to show it as a string.
You are not interested in zoom, you are interested in *zoom
Since *zoom is a char - Watch window will show you the contents of the char
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
Ok, now it's clear. So CODE::BLOCKS IDE interprets the zoom as array. I see now. Thanks
OK, so this should be correct:
Typecast to fit integer to char. The allowed values are smaller then 128 so this is OK.Code:char * zoom = &(w->task.temp.blocksInfo[w->task.temp.square_no].z); *zoom = (char) atoi(*s);
This only suppresses the compiler warning, if any. If char is signed and your assumption does not hold, the behaviour is implementation defined.Originally Posted by barracuda
You might want to store the return value of atoi in a temporary int variable anyway, then write an assertion to document your assumption, and only then assign that int variable to *zoom.Originally Posted by barracuda
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)