|
-
October 16th, 2009, 04:12 AM
#1
C pointer explanation
Am I correct in description of the following code
Code:
int array[5]={0};
int *ptr=array;
...
scanf("%d", &*(ptr + i) );/* Store input at the address of the value at (ptr + i)*/
Does the comment correcty describe what I am doing??
-
October 16th, 2009, 04:29 AM
#2
Re: C pointer explanation
 Originally Posted by g.eckert
Code:
scanf("%d", &*(ptr + i) );/* Store input at the address of the value at (ptr + i)*/
Taking the address of a dereferenced pointer doesn't make much sense
I'd use
Code:
scanf("%d", ptr + i );/* Store input at the address of the value at (ptr + i)*/
Kurt
-
October 16th, 2009, 04:40 AM
#3
Re: C pointer explanation
I swear I used
Code:
scanf("%d", (ptr + i) );
and the compiler complained about something. It is working now though
Last edited by g.eckert; October 16th, 2009 at 04:42 AM.
-
October 16th, 2009, 04:56 AM
#4
Re: C pointer explanation
 Originally Posted by g.eckert
I swear I used
Code:
scanf("%d", (ptr + i) );
and the compiler complained about something. It is working now though
If it's 'C' you're compiling and not C++, the compiler should complain that scanf() has not been defined, but in 'C", it will allow the compile to be successful (since missing function declarations are OK in 'C"). You are missing the include for <stdio.h> in your sample.
That's why when posting code, you better post everything, including the headers that you included. If it really is 'C' and not C++ that you're compiling, a missing header can make the difference between a program that compiles OK and works when run and a program that compiles OK and fails to run correctly.
Regards,
Paul McKenzie
-
October 16th, 2009, 12:55 PM
#5
Re: C pointer explanation
Yes it is ' C ' that I am using. I could not find the "Plain C" board.
I was looking for clarification on what I was doing with the pointers and derefrencing. Making sure that I was describing the statement correctly. Thanks guys.
-
October 16th, 2009, 01:18 PM
#6
Re: C pointer explanation
 Originally Posted by ZuK
Taking the address of a dereferenced pointer doesn't make much sense
Not for a pointer specifically, no. However, if you were dealing with a more general iterator in a C++ templated method, the &* trick would ensure you had an actual address to work with.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|