Thread: newbie structure question...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    12

    newbie structure question...

    howdy

    i'm a python/java guy who's going to attempt to pick up C/C++, and so far it's pretty straightforward. I do have one question regarding structures...

    in all the examples i've seen, when a structure is created, it never initializes the variables to values, it just declares the variable and it's type. is it possible to initialize a variable within a structure when the structure is created?

    here's an example of what i mean:
    Code:
    struct ink {
        char name[60] = "inkedmn";
        int age;
    };
    will that work? (i'm at work and can't test it, i was just curious)

    thanks!
    Last edited by inkedmn; 04-17-2003 at 11:57 AM.
    Brett Kelly

  2. #2
    cereal killer dP munky's Avatar
    Join Date
    Nov 2002
    Posts
    655
    nope, cant initiallize stuff inside of a struct
    guns dont kill people, abortion clinics kill people.

  3. #3
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078

    Re: newbie structure question...

    Originally posted by inkedmn
    howdy

    i'm a python/java guy who's going to attempt to pick up C/C++, and so far it's pretty straightforward. I do have one question regarding structures...

    in all the examples i've seen, when a structure is created, it never initializes the variables to values, it just declares the variable and it's type. is it possible to initialize a variable within a structure when the structure is created?

    here's an example of what i mean:
    Code:
    struct ink {
        char name[60] = "inkedmn";
        int age;
    };
    No, however, in C++ you can initialize to default values with constructors.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    12
    ok, thanks

    is the same true of unions?
    Brett Kelly

  5. #5
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    [QUOTE]Originally posted by inkedmn
    ok, thanks

    is the same true of unions?
    [/QUOTE

    Yes I'm quite sure, I tried jere, and it didn't work.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    12
    right on, thanks all...

    come to think of it, can anybody give me an example of when it might be appropriate to use a union or structure? AND () when it would be more appropriate to use one or the other (since, to a newb like myself, they appear to be very similar) ?
    Last edited by inkedmn; 04-17-2003 at 12:33 PM.
    Brett Kelly

  7. #7
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    170
    You can initialize a structure by doing the following:
    Code:
    #include <stdio.h>
    
    struct ink {
        char name[60];
        int age;
    };
    
    struct ink ink1 = {
         "HELLO1",
         102	
    };
    
    struct ink ink2 = {
         "HELLO2",
         88	
    };
    
    main()
    {
         printf("%s  %d\n", ink1.name, ink1.age);
         printf("%s  %d\n", ink2.name, ink2.age);
         return 0;
    }

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005

    Re: newbie structure question...

    Originally posted by inkedmn
    in all the examples i've seen, when a structure is created, it never initializes the variables to values, it just declares the variable and it's type. is it possible to initialize a variable within a structure when the structure is created?
    You can initialize an object, but not a type.
    Code:
    #include <stdio.h>
    
    struct ink {  /* type */
        char name[60];
        int age;
    };
    
    int main( void )
    {
        struct ink /* type */ svar /* object */ = {"inkedmn"}; /* initializer */
        union {/* type */
            int  i;
            char c;
        } uvar /* object */ = { 42 }; /* initializer */
        printf("svar.name = \"%s\"\n", svar.name);
        printf("uvar.i = %d\n", uvar.i);
        return 0;
    }
    
    /* my output
    svar.name = "inkedmn"
    uvar.i = 42
    */
    A search might turn up some info about structs/unions.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    12
    ok, so structures seem like very basic classes (i.e., without any behaviours, just attributes). so, something like this...

    Code:
    #include <stdio.h>
    
    struct person {
        char name[25];
        int age;
        char email[35];
    };
    
    struct person inkedmn = {
        "Brett Kelly",
        24,
        "[email protected]"
    };
    
    int main(void) {
        printf("%s\n%d\n%s\n", inkedmn.name, inkedmn.age, inkedmn.email);
        return (0);
    }
    that's off the top of my head, but does that seem like it would work?
    Last edited by inkedmn; 04-17-2003 at 01:53 PM.
    Brett Kelly

  10. #10
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    it would work

    Fo'sure

  11. #11
    Registered User
    Join Date
    Apr 2003
    Posts
    12
    excellent
    Brett Kelly

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    In C, yes, structures are in essence classes. They're actually just new object types which are typically just a combination of one or more other data types clustered together.

    In C++, structures are exactly the same thing as classes, except that the default access method is public instead of private.

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

  13. #13
    Registered User
    Join Date
    Apr 2003
    Posts
    12
    Originally posted by quzah
    In C++, structures are exactly the same thing as classes, except that the default access method is public instead of private.
    i'm a little unclear as to what you mean by the default access method for a structure. is there another method to set structure values beside either using the "constructor" (or whatever the correct name is) or by struct.key = value ? how are those two methods private?

    (not trying to be antagonistic, i'm just curious )
    Brett Kelly

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I was a bit unclear with my wording. What I mean is:
    Code:
    C++
    
    class myclass
    {
        signed int myvar;
    };
    
    myclass x;
    
    x.myvar = 10; //invalid. myvar is private
    
    struct mystruct
    {
        signed int myvar;
    };
    
    mystruct y;
    
    y.myvar = 10; //valid. myvar is public
    That's what I was trying to illustrate. That is the only difference between classes and structures in C++.

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

  15. #15
    Registered User
    Join Date
    Apr 2003
    Posts
    12
    so, are there no access modifiers in C++?

    in Java (my only reference point for C/++ syntax), class members/behaviors have access modifiers, like:

    Code:
    public class JavaClass {
        private int memberInt;
        protected String memberStr;
    
        public static void main(String[] args) {
            System.out.println("Main method");
       }
    }
    so, memberInt would only be available to methods within JavaClass, memberStr is only accessible by JavaClass and it's subclasses, and main can be called by anybody/thing.

    does C/C++ have an equivalent?

    [edit]

    i forgot there's also "protected" class members/behaviours that are accessible only in the same package as the class they exist in (and they're denoted by the absence of an access modifier)
    Last edited by inkedmn; 04-17-2003 at 03:36 PM.
    Brett Kelly

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newbie: array question :(
    By cstudent in forum C Programming
    Replies: 2
    Last Post: 04-09-2008, 06:46 AM
  2. pointer to structure question
    By matthughes in forum C Programming
    Replies: 8
    Last Post: 05-19-2007, 01:07 AM
  3. Structure and Linked List User Input Question
    By kevndale79 in forum C Programming
    Replies: 16
    Last Post: 10-05-2006, 11:09 AM
  4. Total Newbie Question
    By Kid A in forum C++ Programming
    Replies: 4
    Last Post: 06-22-2006, 05:36 AM
  5. structure question
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 05-17-2002, 09:17 PM