Pointers to Structures and arguments

This is a discussion on Pointers to Structures and arguments within the C Programming forums, part of the General Programming Boards category; hi, i was wondering could someone help. i want my program to be able to input the values of a ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    42

    Pointers to Structures and arguments

    hi,

    i was wondering could someone help.
    i want my program to be able to input the values of a structure.
    then using a pointer be able to print out and edit specific values of the structure.
    ive made an attempt but i dont seem to be passing the pointer into my print function correctly. the instance of the struct has to be global so i wasnt sure if i should write it in or outside of the main class.
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    int main()
    {
    	/* Structure declaration */
    	struct Struct1
    	{
    		short int shortInt;
    		long int longInt;
    		char string[64];
    		float floating;
    	};
    	struct Struct1 G_Instance; /* Structure variable declarations */
    	/* Initialize the structure variable from input data */
    	
    	printf("Please enter a short integer value, an instance of Struct1: ");
    	scanf("%d", &G_Instance.shortInt );			/* Read the first value for the instance of struct1 */
    
    	printf("Please enter a long integer value, an instance of Struct1: ");
    	scanf("%ld", &G_Instance.longInt );			/* Read the second value for the instance of struct1 */
    
    	printf("Please enter a string, an instance of Struct1: ");
    	scanf("%s", &G_Instance.string );			/* Read the third value for the instance of struct1 */
    
    	printf("Please enter a floating point number, an instance of Struct1: ");
    	scanf("%lf", &G_Instance.floating );			/* Read the fourth value for the instance of struct1 */
    
    	struct Struct1 *pStruct;
    
    	pStruct = &G_Instance;
    
    }
    
    	void printFunction(struct Struct1 *pStruct)
    	{
    	
    		printf("/n %d ", pStruct->shortInt );
    
    	};

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    "Global" _means_ "outside of main", so that should answer your first question right there. The way you have it written, your printFunction doesn't even know what a struct Struct1 is, since there's no definition for it in scope.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    Some things to do:

    1. Make main return an integer value (you said it would return an int).
    2. Don't put the struct declaration inside a function. Your print function can't see it that way, and doesn't know what it is. Put it just below your #include statements.
    3. Use \n for a new line, not /n.
    4. Move your print function above main or put a prototype up there so main knows for sure how to call it.
    5. Don't use the & with char arrays. The name of the array servers as a pointer already.
    6. Read the documentation for scanf:
    %d is not the conversion specifier for a short, it's for a regular int.
    %lf is not for floats, it's for doubles.

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    42
    Thanks so much, my code looks so much better now. it executes and runs the main function just fine, but how does it know to return to the top to run the print function? at the moment the program just asks for the data, and then does not print it back.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    You need to call the print function after you get all of your input, like so:
    Code:
    printFunction(&G_Instance);

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    42
    I get an error saying print function does not exist?

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    42
    oh wait, it works now... thanks for all your help!

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    That was #4 from my list of things to do. The compiler goes from top to bottom. When it gets to the line where you call printFunction, it has never seen that name before, so it says "You're calling something that doesn't exist, or that I don't know about yet". Either move the whole of printFunction above the main function, or put a prototype above the main function. Read up on functions and prototypes here: Cprogramming.com Tutorial: Functions.

  9. #9
    Registered User
    Join Date
    Oct 2010
    Posts
    42
    anduril462, is there a way i can return to the main function where i left off, after executing the printFunction?

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    That happens automatically with every function call. How do you think your program knows where to go after you call printf or scanf? I strongly recommend you read any text books you may have and the tutorials here to grasp the basics.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function arguments, pointers to a structure...
    By \007 in forum C Programming
    Replies: 13
    Last Post: 12-07-2010, 03:26 AM
  2. Problems with Nested Structures and Arrays of Structures
    By Ignoramus in forum C Programming
    Replies: 4
    Last Post: 03-02-2010, 01:24 AM
  3. Pointers to structures & functions arguments
    By lautarox in forum C Programming
    Replies: 4
    Last Post: 11-17-2008, 12:27 PM
  4. Pointers to structures
    By shininghelmet in forum C Programming
    Replies: 20
    Last Post: 09-11-2008, 12:18 PM
  5. pointers to structures (2)
    By templarz in forum C Programming
    Replies: 3
    Last Post: 08-15-2006, 05:56 PM

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