Initializing an array inside of a structure

This is a discussion on Initializing an array inside of a structure within the C Programming forums, part of the General Programming Boards category; Hello, I have been tried to do this but I keep getting segmentation faults. Code: typedef struct { int *v; ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Location
    Portugal
    Posts
    18

    Initializing an array inside of a structure

    Hello,

    I have been tried to do this but I keep getting segmentation faults.

    Code:
    typedef struct
    {
       int *v;
       int x;
    } st;
    If I declare on main() a pointer to st, like st *s, how can I acess both variables in st?

    To access the regular int, I did it this way: s->x=...

    How to declare v with n elements, using malloc?

    Thank you.
    Last edited by Lima; 06-06-2009 at 06:20 PM.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    You have to allocate memory for the struct before you allocate memory for v.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,625
    Code:
    st s;
    s.n = 100;
    s.v = malloc(sizeof(your_type) * s.n);
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    9
    but this declaration :
    s.v = malloc(sizeof(your_type) * s.n);
    malloc fn. return a void pointer so u have to make a cast type by writing
    s.v=(int *)malloc(sizeof(your_type) * s.n);

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,625
    Not in C. In C++ you do, but in C++, you use new instead of malloc.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,753
    Quote Originally Posted by mostafa faisal
    malloc fn. return a void pointer so u have to make a cast type by writing
    s.v=(int *)malloc(sizeof(your_type) * s.n);
    Pointers to void are implicitly convertible to pointers to object types, hence the cast is unnecessary.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    If your compiler complains about the lack of cast for malloc(), either one of the following issues have occured:

    1. You forgot to #include <stdlib.h>
    2. Your compiler is compiling as C++.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with arrays inside a structure
    By babu in forum C Programming
    Replies: 4
    Last Post: 07-12-2007, 09:35 AM
  2. Realloc problems with sturcture array inside structure
    By daveyand in forum C Programming
    Replies: 2
    Last Post: 03-29-2004, 05:48 AM
  3. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 11:09 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM

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