CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2009
    Location
    USA
    Posts
    68

    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??
    Google is your friend.

  2. #2
    Join Date
    Oct 2002
    Location
    Austria
    Posts
    1,284

    Re: C pointer explanation

    Quote Originally Posted by g.eckert View Post
    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

  3. #3
    Join Date
    Feb 2009
    Location
    USA
    Posts
    68

    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.
    Google is your friend.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: C pointer explanation

    Quote Originally Posted by g.eckert View Post
    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

  5. #5
    Join Date
    Feb 2009
    Location
    USA
    Posts
    68

    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.
    Google is your friend.

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: C pointer explanation

    Quote Originally Posted by ZuK View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured