29

When I'm passing a string to the function sometimes I use

char *functionname(char *name[256])

and sometimes I use it without pointers (for example:

char functionname(char name[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.

Adam Zalcman
  • 26,643
  • 4
  • 71
  • 92
Zen Cicevic
  • 311
  • 1
  • 3
  • 5

3 Answers3

57

The accepted convention of passing C-strings to functions is to use a pointer:

void function(char* name)

When the function modifies the string you should also pass in the length:

void function(char* name, size_t name_length)

Your first example:

char *functionname(char *name[256])

passes an array of pointers to strings which is not what you need at all.

Your second example:

char functionname(char name[256])

passes an array of chars. The size of the array here doesn't matter and the parameter will decay to a pointer anyway, so this is equivalent to:

char functionname(char *name)

See also this question for more details on array arguments in C.

Adam Zalcman
  • 26,643
  • 4
  • 71
  • 92
  • 1
    Is there any harm if the function receives the string the way : char functionname(char string[256]) – Akay May 13 '16 at 19:08
  • `void function(char* name)` is thus only applicable when it does not modify the C-string? Wouldn't it make sense to declare it `const`? As for most, you're sending a pointer that should also be const, since you won't be changing the pointer itself? – Paul May 29 '16 at 14:03
  • `char *functionname(char *string name[256])` is a syntax error, not "passes an array of pointers". – M.M Jul 26 '17 at 03:32
  • where is the string actually stored? if we just do `functionname("this is a string")` where will the string be stored? are we sure it will not overwrite anything as we didn't give a length to the space we want to allocate. –  May 19 '20 at 11:51
3

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 chars 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.

Nicola Musatti
  • 17,834
  • 2
  • 46
  • 55
0

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];.

Anickyan
  • 404
  • 2
  • 10
  • #include #include void StringSort(char S[5][30]); void main() { char S[5][30]; int i; printf("Unesi 5 stringova:\n"); for(i = 0; i < 5; i++) gets(S[i]); printf("\nUneseni stringovi su:\n"); for(i = 0; i < 5; i++) puts(S[i]); StringSort(S); printf("\nStringovi sortirani po broju znakova su:\n"); for(i = 0; i < 5; i++) puts(S[i]); } void StringSort(char S[5][30]) { char pS[30]; int i, j; for(i = 0; i < 4; i++) for(j = i + 1; j < 5; j++) if(strlen(S[i]) > strlen(S[j])) { strcpy(pS, S[i]); strcpy(S[i], S[j]); strcpy(S[j], pS); } – Zen Cicevic Jun 16 '13 at 09:39
  • 3
    An array is *not* a pointer. for example, `sizeof(arr)` is different for an array and a pointer. And `new` is C++, not C. – interjay Jun 16 '13 at 09:43
  • @interjay, sorry. Didn't think about that. You can of course use malloc() instead. – Anickyan Jun 16 '13 at 09:44
  • #include #include #include char *fn(char *s1,char *s2); int main() { char a1[20],a2[20]; printf("Unesi string:"); gets(a1); printf("Unesi drugi string:"); gets(a2); printf("Prvi po abecednom redu je %s",fn(a1,a2)); } char *fn(char *s1,char *s2) { if(strcmp(s1,s2)<0) return s1; else return s2; } – Zen Cicevic Jun 16 '13 at 09:44
  • BOTH OF THESE PROGRAMS WORK BUT IN ONE I DIDN'T USE POINTERS AND IN OTHER I DID. LOOK AT THE LINKS http://tny.cz/809ae075 http://tny.cz/60dc68cf – Zen Cicevic Jun 16 '13 at 09:47
  • An array is not a pointer. An array VARIABLE is a pointer to the first element of an array. – Matthew Oakley Jul 26 '17 at 03:28
  • An array is not a pointer. In most cases, array names are just converted to pointers. Refer this link to know more about it. https://stackoverflow.com/questions/1641957/is-an-array-name-a-pointer – Sparkzz Mar 06 '18 at 17:44