Thread: Pointer to struct

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    40

    Pointer to struct

    Not very explaining title maybe, but don't know how to explain shortly.

    I'm trying to create an array of structs, but as I'm not very good at structs so I'm getting some problems. What I want to do is to have an array (using pointers as I don't know what the size of the array will be) of a struct containing two fields.

    So it's supposed to be an array containing a number of structs, where the struct contains a char* and an in.

    I tried it with the below testprogram, but I get segmentation-fault, testing with the printf I discovered that the temp-structure works, but it seems that I'm accessing or populating the processes the wrong way.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    struct process {
      char* name;
      int pid;
    } *processes;
      
    main()
    {
    
      processes=malloc(2*sizeof(*processes));
      char* string;
      //  int temp;
      int x;
      struct process temp;
      temp.name="Testing";
      temp.pid=25;
      processes[0]=temp;
      struct process temp2;
      temp2.name="structs";
      temp2.pid=35;
      
      processes[1]=temp2;
      printf("Name: %s\n",temp.name);
    
      for (x=0;x<2;x++)
        {
          printf("Pid: %d Name: %s\n",processes[x].pid,processes[x].pid); // This fails (or maybe the assigning)
        }
      free(processes);
      exit(0);
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You should be doing:
    Code:
    processes[0].pid = 25;
    processes[0].name = malloc( somesize );
    strcpy( processes[0].name, "somename" );
    You can assign a string literal to the pointer instead:
    Code:
    processes[0].name = "hello world";
    But you're generally better off not.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    40
    Thanks, but it doesn't work here for some reason. Now I'm doing (in my program I will use variables for the string and the size of it). I've tried both with the number of chars, and with number of chars +1 to make room for null-termination, but I'm still getting segmentation fault in the same place (I also tried assigning directly):

    Code:
      processes[0].pid=25;
      processes[0].name=malloc(8*sizeof(char));
      strcpy(processes[0].name,"Testing");
      processes[1].pid=35;
      processes[1].name=malloc(9*sizeof(char));
      strcpy(processes[1].name," structs");
    If it matters, I'm compiling with cc in Ubuntu 8.04 (it seems to be linked to gcc).

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    int main( void )
    {
        struct foo
        {
            char *c;
            int  x;
        } *bar;
        
        bar = malloc( 2 * sizeof *bar );
        if( bar )
        {
            size_t x;
            for( x = 0; x < 2; x++ )
            {
                bar[ x ].x = x;
                bar[ x ].c = malloc( 13 );
                if( bar[ x ].c )
                    strcpy( bar[ x ].c, "hello world\n" );
            }
            
            for( x = 0; x < 2; x++ )
            {
                printf( "bar[ %d ].x = %d\nbar[ %d ].c = %s",
                    x, bar[ x ].x, x, bar[ x ].c );
                free( bar[ x ].c );
            }
            
            free( bar );
        }
        return 0;
    }

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    40
    Thanks to quzah's example I found the bug. I don't know if I did things right from the start, but I discovered something that it seems everyone missed. I try to read processes[x].pid as a string in the for-loop which is most likely what's causing the segmentation fault :-/ When quzah's program ran on my computer with the same functionality I discovered this since I knew then that my program "should" work.

    Thanks for the help.

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    40
    Hello again!

    It seems that I have received a problem when I added functionality using the above. I now created a function for adding a new "processes" to the array, but I'm getting segmentation fault, but I don't understand why. Can anyone see a problem? Here is the testprogram I'm using:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <string.h>
    
    struct process {
      char* name;
      int pid;
    } *processes,*temp;
    int number;
    
    int addProcesses(int pid, char* newname)
    {
      int x;
      if (number==0)
        {
          number++;
          temp=malloc(number*sizeof(*processes));
          temp[0].name=newname;
          temp[0].pid=pid;
          return(temp);
        }
      number++;
      printf("Adding number %d\n",number);
      temp=malloc(number*sizeof(*processes));
      for (x=0;x<(number-1);x++)
        {
          temp[x]=processes[x];
        }
      temp[x].name=newname;
      temp[x].pid=pid;
      free(processes);
      processes=temp;
      free(temp);
      return(0);
    }
    
    main()
    {
      int x=0;
      number=0;
      processes=addProcesses(35,"Testing");
      processes=addProcesses(25," structs");
      processes=addProcesses(15," again!");
      for (x=0;x<number;x++)
        printf("Pid: %d, Name: %s\n",processes[x].pid,processes[x].name);
      exit(0);
    }
    By using printf-statements around the function (removed most of them before publishing here), I discovered that the segmentation fault occurs when I add the third string for some reason. As before I will use variables/pointers in stead of fixed strings in the end program, but I assume the functionality will be the same. I've tried without freeing also, but it didn't seem to make a difference.

    Also, I put temp as another global variable as I couldn't figure out how to create a temp-array inside of the function and the returning it, but it wouldn't compile. Is that not possible (not as important as the main problem)?

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    40
    Maybe I should have written the explanation above the code instead of below, so I'll write it again, on it's own:

    As I wrote above, I get a segmentation fault when I try to add the third string. And it occurs on the assignment in the addProcesses-function.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why don't you compile with warnings on? That would save you a lot of time.
    Code:
     processes=addProcesses(35,"Testing");
      processes=addProcesses(25," structs");
      processes=addProcesses(15," again!");
    Bad!
    Code:
    struct process {
      char* name;
      int pid;
    } *processes,*temp;
    
    int addProcesses(int pid, char* newname)
    Figure out what's wrong yet?


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer problem or so...
    By TL62 in forum C Programming
    Replies: 19
    Last Post: 01-12-2008, 11:45 PM
  2. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  3. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM