Thread: easy question again

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    5

    easy question again

    please help me.
    Q.
    Write a function init that initialises a structure theStruct
    defined as

    typedef struct theStruct {
    int myInt;
    float myFloat;
    char *myString;
    }

    At the end of init, myString should be completely separate from the parameter. Marks will be awarded for correctness and sensible variable and parameter names and return types. Trace the call init(7, 3.2, "Linux 4 eva");

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    And what is your question?

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    typedef struct theStruct {
    int myInt;
    float myFloat;
    char *myString;
    }
    This is missing two things, the new name created by typedef and a closing semicolon.

    >myString should be completely separate from the parameter
    Look into dynamic memory allocation with malloc.

    >Marks will be awarded for correctness and sensible variable and parameter names and return types.
    I hope you weren't expecting us to do your homework for you. This should be relatively easy, only the allocation and copying of the third argument to myString will give you any problems. Give it a shot and if you get stuck, we'll help more.

    -Prelude
    My best code is written with the delete key.

  4. #4
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Code:
    int init(theStruct* ini, float flo, char* str)
    {
           ini.myFloat = flo;
           
           if(str!=NULL)
           {
                   if( !(myString=(char*)malloc(strlen(str)+1)) )
                   {
                           fprintf(stderr,"Mem Alloc Error!!");
                           exit(1);
                   }
                   strcpy(ini.myString,str);
                   return 0;
           }
           else
                   return 1;
    }
    Note that if 'ini' was declared a pointer to a struct, then the
    '-> notation' must be used!

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Note that if 'ini' was declared a pointer to a struct, then the
    '-> notation' must be used!
    Then why didn't you use it in your sample code? To point it out:
    >ini.myFloat = flo;
    >strcpy(ini.myString,str);
    are both incorrect.

    >if( !(myString=(char*)malloc(strlen(str)+1)) )
    What is myString? Did you mean ini->myString
    You don't need to cast the return from malloc().

    >exit(1);
    Maybe the function should return something denoting a failure, rather than just stop the whole program in mid flight.

    And isn't str a reserved word? I'm not meaning to start anything.....
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    I agree that 'myString' should've been ini->myString.

    I don't understand why you say that ini.myString is incorrect though.

    Just because the parameter 'ini' is a pointer to a struct, doesn't mean that it was declared as a pointer to struct!

    Eg.

    theStruct ini;
    theStruct* ptr=(theStruct*)malloc(sizeof(theStruct));

    If the structure that was passed to the function was
    declared as the first one above, then i don't see the
    "ini.myString" notation is wrong.

    and no str is not a reserved word.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Hammer

    Then why didn't you use it in your sample code? To point it out:
    >ini.myFloat = flo;
    >strcpy(ini.myString,str);
    are both incorrect.
    Actually, Hammer, you're wrong here.

    [edit]
    I should learn to ****ing read...
    [HUGE SNIP]
    [INSERT FOOT IN MOUTH]

    int init(theStruct* ini, float flo, char* str)
    The reason Hammer is right, and why I failed to read it and erroniously "corrected" Hammer, was because we both neglected the fact that: YOU PASS YOUR STRUCTURE AS A POINTER!
    [/edit]

    Quzah.
    Last edited by quzah; 07-03-2002 at 03:36 PM.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Actually, Hammer, you're wrong here.

    I should learn to ****ing read...
    [HUGE SNIP]
    [INSERT FOOT IN MOUTH]


    quote:
    --------------------------------------------------------------------------------

    int init(theStruct* ini, float flo, char* str)

    --------------------------------------------------------------------------------



    The reason Hammer is right, and why I failed to read it and erroniously "corrected" Hammer, was because we both neglected the fact that: YOU PASS YOUR STRUCTURE AS A POINTER!
    I'm not 100% on what you're saying here.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by The Dog

    I'm not 100% on what you're saying here.
    1) I posted that Hammer was wrong. I wrote a code sample to prove it.
    2) I hit submit.
    3) I realized that you were wrong, and Hammer was right, because your function takes a pointer to a structure.
    4) I quickly edited my post (hopefully before anyone actually saw it!) and corrected myself.

    Bottom line: Hammer's right. You're wrong. When you use a pointer to a structure, it's members are accessed via ->

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

  10. #10
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Bottom line: Hammer's right. You're wrong. When you use a pointer to a structure, it's members are accessed via ->
    OOPS, maybe you don't understand.

    Members are only accessed that way if the structure was declared a pointer, not if a function accepts a pointer.

    The name of the structure is a pointer to the structure even if it wasn't declared a pointer to a struct!!!!

    JUST BTW!!!

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by The Dog


    OOPS, maybe you don't understand.

    Members are only accessed that way if the structure was declared a pointer, not if a function accepts a pointer.

    The name of the structure is a pointer to the structure even if it wasn't declared a pointer to a struct!!!!

    JUST BTW!!!
    Bull****.

    Code:
    #include <stdio.h>
    struct mystruct { int d; };
    
    void myfunction( struct mystruct *m )
    {
        printf("%d", m.d ); //won't compile
        printf("%d", m->d ); //compiles fine
    }
    
    int main( void )
    {
        struct mystruct x;
        x.d = 10;
    
        myfunction( &x );
        return 0;
    }
    I am mistaken? I think not.

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

  12. #12
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >OOPS, maybe you don't understand.
    You gotta be joking ! quzah, not understand??

    >Members are only accessed that way if the structure was declared a pointer, not if a function accepts a pointer.
    Wrong. If the structure was declared as a pointer, then there isn't actually a structure in existence, only a pointer. That's the point of a pointer. Now, I'm confusing you more Look:

    Code:
    /* this defines a structure, it does NOT create on for you to use */
    struct mystruct
    {
    	int i;
    };
    
    /* This is a pointer to a struct mystruct.  It is NOT a struct */
    struct mystruct *p;
    
    /* This is a struct mystruct.  Now, and only now, does one exist for you to use */
    struct mystruct ss;
    
    /* Now set the pointer to the struct that does actually exist */
    p = &s;
    
    /* now apply some values using the 2 different access methods */
    ss.i = 10;
    p->i = 20;
    >The name of the structure is a pointer to the structure even if it wasn't declared a pointer to a struct!!!!
    What?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  13. #13
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    FIXED:
    My bad, i was reffering to structure arrays, sorry for all that crap.

  14. #14
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    I'll ignore everything else since Hammer and quzah seem to have covered it, but...

    >and no str is not a reserved word.
    I've gone over this countless times since I started programming in C, and the above statement is correct if you want to be truly pedantic. The ISO C standard states that any identifier starting with "str" and followed by a lower case character is reserved for future additions. So string is reserved, but str is not. This is probably where all of the confusion began and I've gotten to the point where if anyone uses just str then I'll point this out as a potential bug since programmers tend to make a lot of changes over the course of development. In my opinion it's better to avoid this altogether rather than confuse people, and I'm sorry if not being clear enough on this point has confused people still.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. easy Vector contructor question
    By noodle24 in forum C++ Programming
    Replies: 5
    Last Post: 04-21-2006, 07:43 PM
  2. Another Embarassingly Easy Question
    By almo89 in forum C Programming
    Replies: 2
    Last Post: 02-11-2006, 04:59 PM
  3. This is hopefully an extremely easy question...
    By rachaelvictoria in forum C Programming
    Replies: 2
    Last Post: 11-07-2005, 01:36 AM
  4. 4 easy question
    By Zeratulsdomain in forum C++ Programming
    Replies: 2
    Last Post: 10-15-2005, 10:43 PM
  5. Easy Question
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 08-12-2002, 12:19 PM