Thread: cannot initialize structure variables

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    11

    cannot initialize structure variables

    Hi,

    i have a problem that is rather basic but since i am not a frequent developper this presents a difficulty to me that i do not understand. So what i have is :


    Code:
    #include <stdio.h>
    #include "w.h"
    
    
    void main(){
    
    WheelCnt *wheel;
    
    wheel->x =0;
    wheel->y = 0;
    
    printf("%d\n",wheel->y);
    
    }
    and w.h that looks like this:


    Code:
    #define MAXWSIZE 200
    
    
    
    typedef struct WheelCnt{   
    	int q[MAXWSIZE];
    	int x;
    	int y ;
    }WheelCnt;
    the error i get when i run the program is :

    Segmentation fault (core dumped)

    and this is because i am trying to do this:

    Code:
    wheel->x =0;
    wheel->y = 0;
    Can someone help


    thank you !!!

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    wheel is just a pointer to a WheelCnt structure, there is no actual structure. The pointer has a garbage value, and you are accessing memory you don't own. That results in undefined behavior, which often manifests as a seg fault.

    You need to declare it as a struct and use the . (dot) operator:
    Code:
    WheelCnt wheel;
    wheel.x = 0;
    or malloc space for it and use the -> operator:
    Code:
    WheelCnt *wheel;
    wheel = malloc(sizeof(*wheel));
    wheel->x = 0;

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You don't have an instance of a structure in that pogram. All you have is a pointer and that pointer points to nowhere. Yes writing through that will cause a crash.
    What are you trying to do? Do you just want a local variable that is a structure, or do you need to dynamically allocate it for some reason?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Warning!
    Code:
    void main(){  }
    That hurts.If i was the compiler i guess i would cry.
    The right thing to write is this
    Code:
    int main(void)
    {
        ..
        return 0;
    }
    Also about the malloc do not forget to include stdlib.h (malloc is written there).And i would suggest you to write (if trying to using the malloc function)
    Code:
    WheelCnt *wheel;
    wheel = malloc(sizeof(WheelCnt ));
    wheel->x = 0;
    because i think that it is more clear,as when you define a struct ,it's like you have your own type of data (like the language has it's primitive types by default int,char,etc).When you malloc memory you use the sizeof(int) -in a more complex statement usually-,so this is why i suggest it.

  5. #5
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    The advantage (in my opinion) to using sizeof object rather than sizeof (type) is that you have one less place to change if you decide to use another type later on: the compiler knows the type of the object.

    Code:
    int *x;
    /* ... */
    x = malloc(100 * sizeof *x); // if the type changes, this line can be left alone
    Code:
    double *x;
    /* ... */
    x = malloc(100 * sizeof *x); // if the type changes, this line can be left alone
    Compare with
    Code:
    int *y;
    /* ... */
    y = malloc(100 * sizeof (int)); // if the type changes, this line must be changed too
    Code:
    double *y;
    /* ... */
    y = malloc(100 * sizeof (double)); // if the type changes, this line must be changed too

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by std10093 View Post
    Also about the malloc do not forget to include stdlib.h (malloc is written there).
    Good call.
    And i would suggest you to write (if trying to using the malloc function)
    Code:
    WheelCnt *wheel;
    wheel = malloc(sizeof(WheelCnt ));
    wheel->x = 0;
    because i think that it is more clear,as when you define a struct ,it's like you have your own type of data (like the language has it's primitive types by default int,char,etc).When you malloc memory you use the sizeof(int) -in a more complex statement usually-,so this is why i suggest it.
    I disagree there, or rather, some people may find it more clear, but it's also more error prone. I prefer the use of *variable_name because it ensures you always malloc the right amount of memory regardless of whether you change the type of variable_name, etc.

    EDIT: Also, I would argue that it is not a terribly complex bit of syntax, really quite simple. variable_name is a pointer, *variable_name is the thing it points to. You want to malloc the size of the the thing it points to. Use of *variable_name is done all the time when returning data to the caller via output parameters to functions.
    Last edited by anduril462; 09-12-2012 at 04:21 PM.

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by std10093 View Post
    Warning!
    Code:
    void main(){  }
    That hurts.If i was the compiler i guess i would cry.
    It is possible that the OP is writing code for an embedded device, in which case "void main()" is correct. I suspect this might be the case due to the variable names - "WheelCnt" might stand for "Wheel Control," and the int members 'x' and 'y' sound like coordinates - but I might very well be wrong.

  8. #8
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    > That hurts.If i was the compiler i guess i would cry.
    hahahaha xD

    But you should seriously check out this link.

  9. #9
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Wonderful things in this post.Bravo to all!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using HDC to initialize data structure
    By derder in forum Windows Programming
    Replies: 3
    Last Post: 07-29-2011, 10:45 PM
  2. structure variables
    By jackson6612 in forum C++ Programming
    Replies: 3
    Last Post: 05-03-2011, 03:20 PM
  3. structure variables
    By sarathius in forum C Programming
    Replies: 8
    Last Post: 04-15-2008, 01:49 AM
  4. Initialize const variables in constructors
    By mvgian in forum C++ Programming
    Replies: 1
    Last Post: 03-21-2006, 09:40 AM
  5. Initialize const member variables in constructors
    By mvgian in forum C++ Programming
    Replies: 5
    Last Post: 03-21-2006, 08:23 AM