Thread: quick pointer question

  1. #1
    Registered User
    Join Date
    Jul 2007

    quick pointer question

    ok so I'm refreshing myself on pointers real quick, I thought I could directly pass the address of a variable to a function and not have to pass it a pointer, am I wrong? But the only way I've gotten this file to work is passing a ptr instead of the address of units. So can it be done without the additional pointer declared in main? (and yes I'm trying to do it without an external variable)

    #include <stdio.h>
    // int units = 0;
    void critic(int *g);
    int main(void)
    	int units = 0;
    	int *ptr;
    	ptr = &units;
    	printf("How many pounds to a firkin of butter?\n");
    	scanf("&#37;d", &units);
    	while(units != 56)
    	printf("You must have looked it up!\n");
    	return 0;
    void critic(int *g)
    	printf("No luck, chummy. Try again.\n");
    	scanf("%d", *g);

  2. #2
    Join Date
    Aug 2002
    Tucson, Arizona
    pass &units instead of ptr, get rid of ptr. Then in critic make that g instead of *g in scanf

  3. #3
    Registered User
    Join Date
    Jul 2007
    doh! right because it's a pointer it's already an address, thanks

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Correct. I think some people are sometimes thrown off by the fact that a pointer is really an address. Therefore when a function requires a pointer, you don't need to actually declare a pointer variable in the calling function necessarily. Sometimes the address of a "normal" variable will do just fine. It's a case of needing to RTM, or equiv. documentation for the function and what it expects.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  2. Quick Pointer question
    By Sentral in forum C++ Programming
    Replies: 3
    Last Post: 05-14-2006, 07:36 PM
  3. Simple pointer question
    By jayznz in forum C Programming
    Replies: 2
    Last Post: 04-04-2006, 11:36 PM
  4. Declaring a Pointer (quick question)
    By viciousv322 in forum C Programming
    Replies: 4
    Last Post: 12-16-2005, 11:27 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM