Programming Tutorials

(追記) (追記ここまで)

Passing pointer to a function in C

By: Emiley J in C Tutorials on 2010年04月16日 [フレーム]

This sample program in C demonstrates the concept of passing a pointer as a parameter to a function. In this program, the function "prompt" accepts two pointers. The second parameter (num) is a pointer to the variable i. Since it is a pointer, the function does not return the value. The value is stored in i itself.

#include <stdio.h>
void prompt(char *msg, int *num);
int main(void)
{
 int i;
 prompt("Enter a num: ", &i);
 printf("Your number is: %d", i);
 return 0;
}
void prompt(char *msg, int *num)
{
 printf(msg);
 scanf("%d", num);
}



(追記) (追記ここまで)


Add Comment

JavaScript must be enabled for certain features to work
* Required information
1000

Comments

No comments yet. Be the first!
(追記) (追記ここまで)
(追記) (追記ここまで)

AltStyle によって変換されたページ (->オリジナル) /