C Pointers

Discussion in 'C' started by prockaz, Apr 22, 2012.

  1. prockaz

    prockaz New Member

    Joined:
    Apr 12, 2012
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    array is int str[25]

    declare and initialize a pointer to the array called str?


    declare a pointer to the array str?

    Using pointer notation , write an expression that will refer to the value of 9?

    Code:
    typedef struct
    {
       char name[20];
       int medal;
       int members;
    } TEAM;
    declare a pointer called ptr to be a pointer to a structure variable of type TEAM?
     
  2. hobbyist

    hobbyist New Member

    Joined:
    Jan 7, 2012
    Messages:
    141
    Likes Received:
    0
    Trophy Points:
    0
    I'm not quite sure if you mean two pointers or just one to the array or the value of 9... is that the 9th element in the array?

    Code:
    int arr[25] = { 0 }, *p1 = arr, *p2;
    p2 = p1; 
    
    printf("%d", *(p2 + 8)); // 9th element
    the struct *would* be something like

    Code:
    TEAM team1 = { "test", 1, 2 }, *p1,
         team2[2] = { { "team1", 1, 1 }, { "team2", 2, 2 } }, *p2, *p3;
    
    p1 = &team1; // single struct
    puts(p1->name);
    
    p2 = team2; // if struct array
    puts((p2 + index_position)->name); // where index_position is target element
    
    p3 = &team2[0]; // specific element within array
    puts(p3->name);
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice