When I'm passing a string to the function sometimes I use
char *functionname(char *string name[256])
and sometimes I use it without pointers (for example:
char functionname(char string[256])
My question is,when do I need to use pointers ? Often I write programs without pointers and it works,but sometimes it doesn't.
Assuming that you meant to write
char *functionname(char *string[256])
Here you are declaring a function that takes an array of 256 pointers to char
as argument and returns a pointer to char. Here, on the other hand,
char functionname(char string[256])
You are declaring a function that takes an array of 256 char
s as argument and returns a char
.
In other words the first function takes an array of strings and returns a string, while the second takes a string and returns a character.
An array is a pointer. It points to the start of a sequence of "objects".
If we do this: ìnt arr[10];
, then arr
is a pointer to a memory location, from which ten integers follow. They are uninitialised, but the memory is allocated. It is exactly the same as doing int *arr = new int[10];
.
Source: Stackoverflow.com