Thread: Question Regarding Scanf/Pointers

  1. #1
    Registered User
    Join Date
    Mar 2020
    Posts
    3

    Question Regarding Scanf/Pointers

    Hi,

    This is one of the questions in my practical:

    "3. Scanf with Pointers:

    Create a file called user_input.c.Inside, write a function called readInts() that reads three ints and one char andexports them to the calling function.The function should obtain these values from the user. The user should be asked toenter the three ints, one after another, and then enter either A or D."

    It then asks for these values to be passed by reference into another function which I created in part 2 of the practical.

    This is what I have so far:

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    
    void readInts();
    }
    	int intOne
    	int intTwo
    	int intThree
    	char charOne
    
    
    	printf("Enter First Integer\n");
    	scanf("%d", &intOne);
    	
            printf("Enter Second Integer\n");
            scanf("%d", &intTwo);
    
    
            printf("Enter Third Integer\n");
            scanf("%d", &intThree);
    
    
            printf("Enter 'A' or 'D'\n");
            scanf(" %c", &charOne);
    }
    I'm really unsure how to pass these values into another function.

    Any help is much appreciated :)

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,972
    So
    Code:
    int main ( ) {
      int myInt;
      scanf("%d",&myInt);
    }
    would become
    Code:
    void readInt(int *myIntPtr) {
      scanf("%d",myIntPtr);
    }
    
    int main ( ) {
      int myInt;
      readInt(&myInt);
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question on scanf();
    By Utopus in forum C Programming
    Replies: 4
    Last Post: 06-15-2007, 07:46 AM
  2. scanf() question.
    By apacz in forum C Programming
    Replies: 12
    Last Post: 09-27-2006, 10:18 AM
  3. Scanf Question
    By shiyu in forum C Programming
    Replies: 4
    Last Post: 01-31-2003, 08:48 AM
  4. pointers and functions, with scanf, please help me
    By Terrance in forum C Programming
    Replies: 2
    Last Post: 11-02-2002, 02:13 PM
  5. Replies: 2
    Last Post: 07-14-2002, 09:08 PM

Tags for this Thread