Like Tree1Likes
  • 1 Post By jimblumberg

problem area - Pointer and Struct

This is a discussion on problem area - Pointer and Struct within the C Programming forums, part of the General Programming Boards category; Hi, I've written a Program like this: Code: #include <stdio.h> #include <stdlib.h> #define STRING_SIZE 30 struct TestStruct { char id; ...

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    3

    problem area - Pointer and Struct

    Hi, I've written a Program like this:


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define STRING_SIZE 30
    
    struct TestStruct
    {
       char id;
       char name[STRING_SIZE];
    };
    
    void ausgabe(struct TestStruct *struct_ptr)
    { 
        printf(" %d %s!\n\n",(*struct_ptr).id, (*struct_ptr).name);     
    }
    
    int main(int argc, char *argv[])
    {
       struct TestStruct test[10];  
    
       test[0].id = 0x61;       
       strcpy(test[0].name, "Name01");
      
       test[1].id = 0x62;
       strcpy(test[1].name, "Name02");
      
       test[2].id = 0x63;
       strcpy(test[2].name, "Name03");
      
       test[3].id = 0x64;
       strcpy(test[3].name, "Name04");
    
       int i;   
       for(i=0;i<=5;i++)
       {
          //printf("%d %s!\n\n", &test[i].id, &test[i].name);    
          ausgabe(&test[i]);
       }
       system("PAUSE"); 
       return 0;
    }
    If I use the Funktion ausgabe() the Program works (97 Name01, 98 Name02 etc.), but when I use these line of Code instead the Funktion ausgabe().
    Code:
    printf("%d %s!\n\n", &test[i].id, &test[i].name);
    The Result is that the .id's have some Values like 2686036 or 2686032 etc.

    Can some one explain me why this happens?

  2. #2
    Registered User
    Join Date
    Mar 2010
    Posts
    535
    Yep:

    Code:
    printf("%d %s!\n\n", &test[i].id, &test[i].name);
    You're using & to take the address of the arguments to printf. %d expects a value, not a pointer to the value. So the number you're seeing is the address that the id is stored at.

    You also do not need to take the address of test.name. The reason that that is working ok is that for %s a pointer is expected. If you do not take the address of the array, the compiler will do it for you and pass a pointer to the first element. If you want to know more about this have a look at the c faq page: Arrays and Pointers

    Code:
    void ausgabe(struct TestStruct *struct_ptr)
    { 
        printf(" %d %s!\n\n",(*struct_ptr).id, (*struct_ptr).name);     
    }
    Here you have a pointer to a struct which you dereference, so you're passing the value, so it works.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    2,674
    You are telling printf to print the address, not the value. You don't need the ampersand (&) in front of the variable names.

    Jim
    venkatsam likes this.

  4. #4
    Registered User
    Join Date
    Jul 2012
    Posts
    3
    It's been a while that I wrote something in C. Thanks for the Link, I'll take a look on that page. Thanks for your answers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with pointer to struct
    By M9000 in forum C Programming
    Replies: 3
    Last Post: 07-31-2009, 11:23 AM
  2. Pointer to struct problem
    By gavio in forum C Programming
    Replies: 10
    Last Post: 08-26-2008, 11:14 AM
  3. pointer/array/struct problem
    By sand_man in forum C Programming
    Replies: 12
    Last Post: 06-08-2004, 09:30 AM
  4. Struct pointer problem
    By fkheng in forum C Programming
    Replies: 10
    Last Post: 08-08-2003, 10:59 AM
  5. struct/pointer problem?
    By Jase in forum C Programming
    Replies: 17
    Last Post: 05-31-2003, 11:09 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21