Thread: pointer to structure

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    347

    pointer to structure

    Hi,
    i am confused while writing this program
    Code:
    #include<stdio.h>
    typedef  struct
    {
        int (*p)[3];
    }can1;
    void function1(can1 *ptr );
    
    int main()
    {
    int array[2][3]=
    {
    {1,2,3},
    {4,5,6},
    };
    can1 cand={&array};
    function1(&cand);
    return 0;
    }
    void function1(can1 *ptr )
    {
     printf("%d\n",(*ptr->p[1])[0]);
    }
    My explanation is something like this *(ptr->p[1]) is a pointer pointing to element 4 and *(ptr->p[1])[0] is the value at that address. But the compiler shows error. what is my mistake.

    Thanks in advance,

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Recall that the operator -> already implicitly dereferences the left operand. Therefore, ptr->p is already the member pointer that points to an array of 3 ints, hence ptr->p[1][0] is an int.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    347
    I am getting the following warnings
    warning: initialization from incompatible pointer type
    warning: (near initialization for 'cand.p') in the line
    Code:
    can1 cand={&array};
    What is the mistake? i am getting the proper output

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Think: what is the type of the variable named array? When this variable is converted to a pointer to its first element, what is the type of the resulting pointer? If you take its address, what is the type of the result?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-26-2011, 10:40 PM
  2. Referencing pointer inside a structure pointer
    By SasDutta in forum C Programming
    Replies: 2
    Last Post: 11-11-2010, 11:33 AM
  3. pointer to structure
    By violatro in forum C Programming
    Replies: 5
    Last Post: 06-01-2010, 04:16 AM
  4. Replies: 9
    Last Post: 06-13-2009, 02:31 AM
  5. Structure Pointer to Structure
    By u_peerless in forum C Programming
    Replies: 1
    Last Post: 01-28-2009, 12:40 AM