print a pointer value in c code example

Example 1: how to print value of pointer in c

#include <stdio.h>
#include <stdlib.h>

void pointerFuncA(int* iptr){
/*Print the value pointed to by iptr*/
printf("Value:  %d\n", *iptr );

/*Print the address pointed to by iptr*/
printf("Value:  %p\n", iptr );

/*Print the address of iptr itself*/
printf("Value:  %p\n", &iptr );
}

int main(){
int i = 1234; //Create a variable to get the address of
int* foo = &i; //Get the address of the variable named i and pass it to the integer pointer named foo
pointerFuncA(foo); //Pass foo to the function. See I removed void here because we are not declaring a function, but calling it.

return 0;
}

Example 2: how to use a pointer as a parameter in c

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void add(int* a, int* b, int* c)
{
    *c = *a + *b;
}
int main()
{
	int a, b, c;
	a = 3;
	b = 5;
	add(&a, &b, &c);
	printf("%d", c);
}

Tags:

Misc Example