Pointers and structures

This is a discussion on Pointers and structures within the C Programming forums, part of the General Programming Boards category; I'm having a little trouble passing values from a structure to a function using pointers. I have defined a structure ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    40

    Pointers and structures

    I'm having a little trouble passing values from a structure to a function using pointers.

    I have defined a structure as such:
    Code:
    struct Struct1 {
        short Shrt1;
        long Lng1;
        char Array1[60];
        double Doub1;
    };
    Then tried to declare a instance of this structure and a pointer to that as such:
    Code:
        struct Struct1 inst1;
        struct Struct1 *ptr;
        ptr=&inst1;
    I have then tried to pass values to a function and use them like this:
    Code:
    void function1(ptr)
    {
        printf("\nShort integer 1: %d", ptr->Shrt1);
        printf("\nLong integer 1: %L", ptr->Lng1);
        printf("\nCharacter Array 1: %s", ptr->Array1);
        printf("\nDouble Precision Number 1: %d", ptr->Doub1);
    
    return;
    }
    But I am getting an error saying that ptr is an invalid type argument. Any ideas where i'm going wrong? Do I need to post more of the code? should function use *ptr instead of ptr as an argument? I'm quite new to C and having trouble getting my head around the correct way to use pointers.

  2. #2
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Try void function1(struct Struct1 *ptr)...and see where it gets you..
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Are you looking to do something like:

    Code:
    void function1(struct Struct1 *ptr);
    That works, you should pass it &inst1. There is no need for a "ptr = &inst1".
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by ineedmunchies View Post
    I'm having a little trouble passing values from a structure to a function using pointers.

    I have defined a structure as such:
    Code:
    struct Struct1 {
        short Shrt1;
        long Lng1;
        char Array1[60];
        double Doub1;
    };
    Then tried to declare a instance of this structure and a pointer to that as such:
    Code:
        struct Struct1 inst1;
        struct Struct1 *ptr;
        ptr=&inst1;
    I have then tried to pass values to a function and use them like this:
    Code:
    void function1(ptr)
    {
        printf("\nShort integer 1: %d", ptr->Shrt1);
        printf("\nLong integer 1: %L", ptr->Lng1);
        printf("\nCharacter Array 1: %s", ptr->Array1);
        printf("\nDouble Precision Number 1: %d", ptr->Doub1);
    
    return;
    }
    But I am getting an error saying that ptr is an invalid type argument. Any ideas where i'm going wrong? Do I need to post more of the code? should function use *ptr instead of ptr as an argument? I'm quite new to C and having trouble getting my head around the correct way to use pointers.
    Your argument in function1 which is ptr is incorrect. You need to have it's type also as in simple functions. Eg
    Code:
    void func(int *p) //we dont write simply 'p' without int *
    {
    then do anything;
    }
    Similar to the above case you should be having the argument's type.
    Code:
    void function1(struct Struct1 *ptr)
    {
        printf("\nShort integer 1: %d", ptr->Shrt1);
        printf("\nLong integer 1: %L", ptr->Lng1);
        printf("\nCharacter Array 1: %s", ptr->Array1);
        printf("\nDouble Precision Number 1: %d", ptr->Doub1);
    
    return;
    }
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  5. #5
    Registered User
    Join Date
    Dec 2009
    Posts
    40
    Ok, thank you. That now compiles ok, but I can't work out how to get the function to be called in the main body.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct Struct1 {
        short Shrt1;
        long Lng1;
        char Array1[60];
        double Doub1;
    };
    
    void function1(struct Struct1 *ptr);
    
    int main()
    {
        struct Struct1 inst1;
        struct Struct1 *ptr;
        ptr=&inst1;
    
        printf("Please enter short integer 1:");
        scanf("%d", &inst1.Shrt1);
        printf("Please enter long integer 1:");
        scanf("%d", &inst1.Lng1);
        printf("Please enter a character string:");
        scanf("%s", &inst1.Array1);
        printf("Please enter a double precision floating point number:");
        scanf("%f", &inst1.Doub1);
    
        function1();
    
        return 0;
    }
    
    void function1(struct Struct1 *ptr)
    {
        printf("\nShort integer 1: %d", ptr->Shrt1);
        printf("\nLong integer 1: %L", ptr->Lng1);
        printf("\nCharacter Array 1: %s", ptr->Array1);
        printf("\nDouble Precision Number 1: %d", ptr->Doub1);
    
    return;
    }
    I don't know what to put in for the arguments of function1 in main?

  6. #6
    Registered User
    Join Date
    Dec 2009
    Posts
    40
    Oh ok, so should &inst1 work as the argument in the main body?

    EDIT: Yea it does, I just have some type issues to correct. Thank you very much

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structures, arrays, pointers, malloc.
    By omnificient in forum C Programming
    Replies: 9
    Last Post: 02-29-2008, 12:05 PM
  2. vector of arrays of pointers to structures
    By Marksman in forum C++ Programming
    Replies: 13
    Last Post: 02-01-2008, 04:44 AM
  3. Structures, and pointers to structures
    By iloveitaly in forum C Programming
    Replies: 4
    Last Post: 03-30-2005, 06:31 PM
  4. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 10:04 AM
  5. Freeing pointers in structures
    By jim50498 in forum C Programming
    Replies: 4
    Last Post: 03-08-2002, 12:53 PM

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