Functions, pointers, structures - can't figure this out Newb

This is a discussion on Functions, pointers, structures - can't figure this out Newb within the C Programming forums, part of the General Programming Boards category; Not sure what is wrong here, I can't seem to compile. I am trying to pass the structure into the ...

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    4

    Functions, pointers, structures - can't figure this out Newb

    Not sure what is wrong here, I can't seem to compile. I am trying to pass the structure into the function to just print the list. I was asked to use "pass by value" instead of structure. Help is much appreciated, I am new to this and just read the chapter...

    Also, is my structure initialization correct? Should I initialize it outside of the main function?

    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define MAX 61
    #define SIZE 5
    
    struct name {
        char fname[MAX];
        char middle[MAX];
        char lname[MAX];
    };
    
    struct sin {
        int snumber;
        struct name info;
    };
    
    void printNames(struct sin * temp[]);
    
    int main(void)
    {
        int i = 0;
        struct sin * ptrData;
    
        struct sin data[SIZE] = {
            {302039823, {"Flossie", "Marie", "Dribble"}},
            {123456789, {"DeMar", "Test", "DeRozan"}},
            {987654321, {"John", "", "Doe"}},
            {736491092, {"Jane", "Cherry", "Dairy"}},
            {282398624, {"Andrea", "James", "Bargnani"}}
        };
    
        //for (i = 0; i < SIZE; i++)
           // ptrData[i] = &data[i];
    
        ptrData = &data;
    
        printNames(ptrData);
    
    
        return 0;
    }
    
    void printNames(struct sin * temp[])
    {
        int i = 0;
    
        for(i = 0; i < SIZE; i++)
        {
            if(temp[i]->info.middle[0] != '\0')
            {
                printf("%s, %s %c. -- %d\n", temp[i]->info.lname, temp[i]->info.fname, temp[i]->info.middle[0], temp[i]->snumber);
            }
            else
            {
                printf("%s, %s %*-- %d\n", temp[i]->info.lname, temp[i]->info.fname, temp[i]->info.middle[0], temp[i]->snumber);
            }
        }
    }

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    I wouldn't call you a newb per say, you've got the indentation/int main/return 0/pretty much everything else correct.

    As for the actual passing by value, you want to declare your function so it can accept an array:
    Code:
    void printNames(struct sin temp[]);
    Then, you can just pass "data" to the function without the pointer:
    Code:
    printNames(data);
    ----

    By the way, your pointer initialization quite correct; because C treats pointers so similarly to arrays, there are two ways to do it:
    Code:
    ptrData = data; /* this is really all you need */
    ptrData = &data[0]; /* it's pointing to the address of the first element, does the same thing */

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    4
    Thanks for the reply memcpy.

    Maybe I am not understanding my exercise properly.

    Here is the question, I am trying to do part b. I did part a and declared my function to take a structure array but I don't/can't do part b. I thought I was suppose to do it using pointers

    Write a program that creates a structure template with two members according to the following criteria:

    a) The first member is a social security number. The second member is a structure with three members. Its first member contains a first name, its second member contains a middle name, and its final member contains a last name. Create and initialize an array of five such structures. Have the program print the data in this format:

    Dribble, Flossie M. -- 302039823

    Only the initial letter of the middle name is printed, and a period is added. Neither the initial (of course) nor the period should be printed if the middle name member is empty. Write a function to do the printing; pass the structure array to the function.

    b) Modify part "a" by passing the structure value instead of the address.

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Am I missing something, or is:
    pass the structure array to the function
    and:
    assing the structure value instead of the address
    the same thing?

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

    The closest thing to "passing by value" in regards to an array is this:
    Code:
    void printNames(struct sin temp[]);
    
    struct sin temp[];
    printNames(temp);
    And passing by reference is this:
    Code:
    void printNames(struct sin *temp[]);
    
    struct sin temp[];
    printNames(&temp);

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    4
    Ok so I am not that stupid then haha. I thought I didn't understand what the question was asking. It is doing something redundant but worded different.

    Either case, I would still like to know why my code does not work why I try to pass by reference. It is very similar to what you mentioned but I get compile issues.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define MAX 61
    #define SIZE 5
    
    struct name {
        char fname[MAX];
        char middle[MAX];
        char lname[MAX];
    };
    
    struct sin {
        int snumber;
        struct name info;
    };
    
    void printNames(struct sin * temp[]);
    
    int main(void)
    {
        int i = 0;
        struct sin * ptrData;
    
        struct sin data[SIZE] = {
            {302039823, {"Flossie", "Marie", "Dribble"}},
            {123456789, {"DeMar", "Test", "DeRozan"}},
            {987654321, {"John", "", "Doe"}},
            {736491092, {"Jane", "Cherry", "Dairy"}},
            {282398624, {"Andrea", "James", "Bargnani"}}
        };
    
        //for (i = 0; i < SIZE; i++)
           // ptrData[i] = &data[i];
    
        //ptrData = data;
    
        printNames(&data);  //changed to &data instead of assigning another pointer but I still can't get it to work
    
    
        return 0;
    }
    
    void printNames(struct sin * temp[])
    {
        int i = 0;
    
        for(i = 0; i < SIZE; i++)
        {
            if(temp[i]->info.middle[0] != '\0')
            {
                printf("%s, %s %c. -- %d\n", temp[i]->info.lname, temp[i]->info.fname, temp[i]->info.middle[0], temp[i]->snumber);
            }
            else
            {
                printf("%s, %s %*-- %d\n", temp[i]->info.lname, temp[i]->info.fname, temp[i]->info.middle[0], temp[i]->snumber);
            }
        }
    }

  6. #6
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Your printf on line 56 is messed up, double check the format string.

    And if your compiler is complaining about passing the struct, you can just typecast it:
    Code:
    printNames((struct sin **)&data);
    In case you're wondering why I'm typecasting to a double pointer not a pointer to an array, it's because, technically, whenever you pass an array in C, you're only passing a pointer to it. Typecasting to a double pointer is perfectly fine in that respect.

  7. #7
    Registered User
    Join Date
    May 2012
    Posts
    4
    Now my program crashes after the type cast

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,758
    Quote Originally Posted by Swift
    Either case, I would still like to know why my code does not work why I try to pass by reference. It is very similar to what you mentioned but I get compile issues.
    You should be aware that this:
    Code:
    void printNames(struct sin * temp[]);
    is different syntax but otherwise identical to:
    Code:
    void printNames(struct sin **temp);
    Now, consider:
    Code:
    printNames(&data);
    data is a struct sin[5]. Therefore &data is a pointer to an array of 5 struct sin objects. Such a pointer is not convertible to a pointer to a pointer to a struct sin. If you pass data instead:
    Code:
    printNames(data);
    Then what you pass is a pointer to a struct sin, since an array will be converted to a pointer to its first element. However, a pointer to a struct sin is still not a pointer to a pointer to a struct sin.

    What I think is the right approach here is to just change printNames to:
    Code:
    void printNames(struct sin *temp);
    or equivalently:
    Code:
    void printNames(struct sin temp[]);
    This way, the printNames(data) call will work, and then you just iterate over the elements of the array via the pointer.

    Quote Originally Posted by memcpy
    And if your compiler is complaining about passing the struct, you can just typecast it:
    That is not a good idea.

    Quote Originally Posted by memcpy
    Typecasting to a double pointer is perfectly fine in that respect.
    It is not fine at all because a pointer to an array is not convertible to a pointer to a pointer. The typecast just suppresses a valid warning message by the compiler.
    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

  9. #9
    Registered User
    Join Date
    Aug 2007
    Location
    MD, USA
    Posts
    71
    Regarding this part b:
    b) Modify part "a" by passing the structure value instead of the address.
    I have not seen that above. All are by address and can be incremented.

    Thoughts:
    Unlike arrays, a struct can be copied by assignment.
    When you pass an object by value a copy is provided to the function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. qsort+structures: Cant figure what's wrong
    By ERJuanca in forum C Programming
    Replies: 3
    Last Post: 10-13-2009, 01:49 PM
  2. Pointers to structures & functions arguments
    By lautarox in forum C Programming
    Replies: 4
    Last Post: 11-17-2008, 11:27 AM
  3. newb to C, cant figure something out.
    By Phate4219 in forum C Programming
    Replies: 16
    Last Post: 03-06-2006, 12:47 AM
  4. Replies: 5
    Last Post: 04-11-2002, 11:29 AM
  5. Newbie Help (Arrays, Structures, Functions,Pointers)
    By tegwin in forum C++ Programming
    Replies: 3
    Last Post: 02-19-2002, 05:29 PM

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