call by reference

Please have a look at the below program
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <stdio.h>


void str_cpy (char* target, char* source) {

	printf("%d\n",*target); //prints 104
	printf("%s\n",target); //prints hello
}

int main() {

 char s1[100] = { "hello" }, s2[100] = { "world" };


 str_cpy(s1,s2);

 return 0;
}


I am getting the output as
104
hello

i guess 104 is the address of the variable , but isn't the address looking a little small ?
My question is why *target is printing 104 and target is printing hello
When i first wrote the program i had predicted the output to be
hello
104 But i was wrong .
104 is ASCII code of symbol 'h' .

This statement

printf("%d\n",*target);

prints character stored in a memory cell *target as an integer number (%d).

In the second statement

printf("%s\n",target); //prints hello

the function interprets target as a pointer to the first symbol of a string literal and outputs all symbols pointed by target until it meets the terminating zero character.

Last edited on
that was some simple logic .... foolish of me , thanks !
Topic archived. No new replies allowed.