Thread: structures

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    7

    structures

    ok,i'm trying to build a program, with socket connections. but i'm having difficulties in structures.
    i can't figure out the problem of this code.
    it brings me Segmation Fault after gcc compiling.
    i hope you will help me with this.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    struct topics {
     int topicID;
     char *baseTopicName;
     struct messages* thema;
    } staticTopics[6];
    
    struct messages {
      char *topicName;
      char *sender;
      char *message;
    
    };
    
    int main(){
         staticTopics[1].thema[1].topicName="aasss";
    
    
       
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    And why do you think thema[1] even exists? Thema is a pointer, and you didn't initialize it, or provide any memory for it to point to, so far as I can see here, so it's not pointing to anything in particular. Trying to follow that pointer leads to Bad Things.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    87
    thema is just a pointer - a piece of memory large enough to hold another memory address, there is not actually any space allocated for any struct messages data types, yet. You need to do this. Say you want N of these. Then you can do,
    Code:
    staticTopics[i].thema = malloc(N * sizeof(struct messages));
    Also note that in C array indexes start at zero. So the elements of staticTopics are [0]-[5].

    Good luck
    Last edited by jason_m; 10-26-2008 at 05:53 PM. Reason: "message"->"messages"

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    7
    thanks jason_m.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Structures within Structures
    By Misko82 in forum C Programming
    Replies: 2
    Last Post: 08-27-2007, 12:25 AM
  3. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  4. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 09:04 AM
  5. Methods for Sorting Structures by Element...
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 09-14-2001, 12:59 PM