working with pointers to structures

This is a discussion on working with pointers to structures within the C Programming forums, part of the General Programming Boards category; I have the next 2 structures: Code: typedef struct { char authors[5][40]; // first and last names of the author ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    31

    working with pointers to structures

    I have the next 2 structures:
    Code:
    typedef struct
    {
    char authors[5][40]; // first and last names of the author
    char title[50]; // title of the publication
    unsigned int year; // year of the publication
    char isbn[15]; // publication code
    } PubEntryT;
    
    
    typedef struct nodeS
    {
    PubEntryT *Item; // pointer to satellite data
    struct nodeS *next; //pointer to next cell
    } NodeST; // type of cells on the singly linked list
    
    NodeST *q;
    char *p;
    How can I give to q->Item->author[0] the value of p?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,848
    First you do
    q = malloc( sizeof *q );

    Then you do
    q->Item = malloc( sizeof *q->Item );

    Then
    strcpy( q->Item->authors[0], p );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    31
    Quote Originally Posted by Salem View Post
    First you do
    q = malloc( sizeof *q );

    Then you do
    q->Item = malloc( sizeof *q->Item );

    Then
    strcpy( q->Item->authors[0], p );
    I'm geeting segmentation fault also if I use this. "Cannot acces memory at adress ..."


    --- SOLVED ---
    Last edited by Delia; 03-21-2010 at 06:48 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers, structures, and malloc
    By lugnut in forum C Programming
    Replies: 24
    Last Post: 10-09-2008, 05:52 PM
  2. Pointers to structures
    By shininghelmet in forum C Programming
    Replies: 20
    Last Post: 09-11-2008, 12:18 PM
  3. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 10:04 AM
  4. working with strings arrays and pointers
    By Nutka in forum C Programming
    Replies: 4
    Last Post: 10-30-2002, 08:32 PM
  5. Replies: 5
    Last Post: 04-11-2002, 12:29 PM

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