Thread: structure info

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926

    structure info

    When you make a structure
    Code:
    struct info{
         int x;
         char *string[];
    };
    then do
    Code:
    struct info new=NULL;
    struct info *ptr;
    ptr=&given;
    what is *string[]'s value and what would an int's and double's and so forth be? I tried with this
    Code:
    printf("The sting is NULL when on the next line\n...%s...",new->name);  /*new->name is a one dimensional array here*/
    and I got a seg fault. I am pretty sure an int would be 0 a float would be 0.0f and a double 0.0, I think a string is '\0'. If I am wrong correct me please. I have no clue on a n-dimension array though thanx
    Last edited by linuxdude; 05-09-2004 at 03:19 PM.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    new is a keyword, so you shouldn't give that name to an object.

    I'm not sure what you're asking, but if you're trying to print a c-string without initializing it (giving it a value) you might end up with segfaults.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    new isn't a keyword in C though(it is just an example for the code I made up). I am initializing it(i think) I know I am initializing the whole struct. What are the values in the struct equal to after that happens

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    I'm confused by what you're asking
    new is pointing at NULL, so that's obvious why its segfaulting

    floats, doubles?
    Where are they in your code

    you print new->name, but there is no name

    char *strings[] isn't a 2D array
    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.

  5. #5
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    I was thinking about doing something. A little experiment. I was wondering what happens when you declare a structure as NULL do all the elements become there equivalent of NULL or do they not exist until you allocate memory for them.
    char *strings[] isn't a 2D array
    Then what do I use to hold an array of strings when malloced?
    you print new->name, but there is no name
    I had that from something I wrote I am sorry name could be any string.

    sorry for the confusion
    Last edited by linuxdude; 05-09-2004 at 06:03 PM.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    When you create an instance of a structre, all members exist. It doesn't matter what you initialize them to, they exist simply because you've created an instance of it. It's lke doing:
    Code:
    int x = 0;
    x exists, what you initialize it to makes no difference, because x already has been created. In the case of a structure, all members exist. Therefore, you're allowed to do whatever you normally could with them. They've all simply (potentially) been set to zero. The string (array) exists, it just potentially holds nothing.

    On an aside, your initial example is just wrong, becaue you haven't made a string. You've attempted to make an array of pointers to characters, but you haven't specified any size for the array.

    So in short, if you're attempting to get an answer, try to at least make your code as legal or compilable as possible, otherwise giving "what if" answers doesn't really do any good if all your code is wrong.

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

  7. #7
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    Sorry for all the confusion. I am thinking of something and I have never done it before. I think I figured a way around my problem with realloc. I'll let you know when I screw up again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help displaying info from structure
    By kisiellll in forum C Programming
    Replies: 6
    Last Post: 04-04-2009, 12:51 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Help doing an e-mail program in c...
    By Tyler_Durden in forum C Programming
    Replies: 88
    Last Post: 01-02-2005, 03:12 PM
  4. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM
  5. Replies: 3
    Last Post: 12-06-2001, 05:30 AM