-1

Even with int foo(char str[]); which will take in an array initialized to a string literal sizeof doesn't work. I was asked to do something like strlen and the approach I want to take is to use sizeof on the whole string then subtract accordingly depending on a certain uncommon token. Cuts some operations than simply counting through everything.

So yea, I tried using the dereferencing operator on the array(and pointer too, tried it) but I end up getting only the first array element.

How can I sizeof passed arguments. I suppose passing by value might work but I don't really know if that's at all possible with strings.

lightburst
  • 231
  • 3
  • 19
  • 2
    read question 7.28 of [the comp.lang.c faq](http://c-faq.com/). Since you're there already, read the whole section ... then the whole faq :-) – pmg Sep 19 '11 at 13:54
  • Many duplicates, e.g. [C sizeof a passed array](http://stackoverflow.com/questions/5493281/c-sizeof-a-passed-array) – Paul R Sep 19 '11 at 14:00

4 Answers4

2

int foo(char str[]); will take in an array initialized to a string literal

That's not what that does. char str[] here is identical to char* str. When an array type is used as the type of a parameter, it is converted to its corresponding pointer type.

If you need the size of a pointed-to array in a function, you either need to pass the size yourself, using another parameter, or you need to compute it yourself in the function, if doing so is possible (e.g., in your scenario with a C string, you can easily find the end of the string).

James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • >>When an array type is used as the type of a parameter, it is converted to its corresponding pointer type.<< However it is still called array type and pointer type by the compiler/debugger or whatever. Also counting the array would defeat my purpose but I suppose I could do that as a last resort. I was aiming for as little operations as possible. – lightburst Sep 19 '11 at 13:55
  • "it is still called array type and pointer type by the compiler/debugger or whatever" In error messages and user-facing output, perhaps. Internally, parameters of type `char[]` and `char*` are exactly the same; there is no difference between them whatsoever. You can use them interchangeably. – James McNellis Sep 19 '11 at 17:58
2

You can't use sizeof here. In C arrays are decayed to pointers when passed to functions, so sizeof gives you 4 or 8 - size of pointer depending on platform. Use strlen(3) as suggested, or pass size of the array as explicit second argument.

Nikolai Fetissov
  • 82,306
  • 11
  • 110
  • 171
  • +1 for suggesting passing array size as an explicit argument -- for my money that's the way to go. – AAT Sep 19 '11 at 13:49
0

C strings are just arrays of char. Arrays are not passed by value in C; instead, a pointer to their first element is passed.

So these two are the same:

void foo(char blah[]) { ... }
void foo(char *blah)  { ... }

and these two are the same:

char str[] = "Hello";
foo(str);

char *p = str;
foo(p);
Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
0

You cannot pass an array as a function parameter, so you can't use the sizeof trick within the function. Array expressions are implicitly converted to pointer values in most contexts, including function calls. In the context of a function parameter declaration, T a[] and T a[N] are synonymous with T *.

You'll need to pass the array size as a separate parameter.

John Bode
  • 119,563
  • 19
  • 122
  • 198
  • You can't pass an ARBITRARY length array as a formal. You can pass a FIXED LENGTH array, if you wrap it in a struct{} ;) But that's usually silly -- if determining the size at run-time is a bother, either pass in the size, or create a struct{} with the size and point as members. – Julie in Austin May 06 '12 at 01:25