October 12th, 2013, 01:38 AM
Problem with Pointers
int *p = a;
why the output executes only one printf statement?? :rolleyes:
October 12th, 2013, 02:00 AM
What output do you get?
What output do you get when you add a newline to those printfs? As in "%d\n".
Also, use "%p" to print out a memory address.
October 12th, 2013, 03:14 AM
Oh, actually i didn't wrote new line \n
that's why both the printf statements got mixed!
Though in the above program is there any difference b/w these:
p = a;
*p = a;
since both of them gives the same results..
October 12th, 2013, 01:28 PM
I figured that, since you did nothing to visually separate the two outputs, you saw the two concatenated together and thought it was just one number. Though you should have been suspicious since the number was so large.
Also, you ignored your compiler's warnings (assuming that you even have warnings turned on). Here is what I got (MinGW gcc on Win7):
That warning for Line 11 ("control reaches end of non-void function") is because you left out the return statement at the end of main(). You are quite correct to have main return int, but then you must also actually return an int. The convention is that if the program reaches the end successfully, then you return a zero. There are other programs and scripts in your computer's system that may need that information, so do get into the habit.
The warning on Line 8 ("int format, pointer arg (arg 2)") is the important one here. This is the line that produced it:
The reason for that warning is that you tell printf to print a decimal integer (with the "%d"), but you don't give it an integer to display, but rather a memory address (AKA "pointer", a concept with which you will become intimately familiar in C). The compiler is warning us that that argument you gave it, a, is a pointer.
Here is the output I got when I ran your program, which I altered ever so slightly by putting a space before the second number:
Do you see that large number? Didn't you wonder what that was about? Well that is the "value" of a, and since the "value" of the name of an array is a pointer -- ie, the address in memory where that array starts -- that address gets converted to its integer equivalent and gets displayed.
As I already told you, pointer values are supposed to be displayed using a different descriptor flag, "%p". That will display the address in hexadecimal, AKA "base 16", a very convenient way to display a binary value, far easier to work with than octal (base 8). And, the compiler won't throw warnings at you since now you are giving it the kind of value that you told it you would give it. Always tell the compiler the truth and always keep your promises to it.
Here is your program with those corrections in it. Please note my use of code tags, which are needed to preserve your code's formatting:
And the output is:
int *p = a;
Quite clearly, they do not give the same results, nor do they even do the same thing.
Originally Posted by alpha090
p is a pointer to int. As an array of int, a is also a pointer to int. Therefore,
p = a;
sets p to point to the array, a. This is a valid and quite normal operation.
*p=a; is not normal nor is it valid, though the compiler, thinking that you are trying to do something clever, will try to do it anyway, but will throw a warning at you (which I got when I inserted that statement into your program):
newb2.c:9: warning: assignment makes integer from pointer without a cast
*p is what's called dereferencing. p points to a location in memory, while *p gives you access to the int value that is stored at that memory address. So then what that statement is trying to do is to store a pointer in an int. That is not a valid thing to do.
But you don't do that in your program. Instead, you had this statement, which is quite different:
That statement declares p to be a pointer to int and it initializes it to point to the array, a. Initializing a variable when you declare it is different from assigning a value to a variable later on in the program.
Comments on this post
October 13th, 2013, 12:07 AM
Thanks a ton for explaining it thoroughly, Now It's Clear.
I am using devcpp on Win 7 (I think it also uses the same compiler MinGW gcc).