sizeof () doesn't work when called for an argument of a function. Ayudarme!

Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;

int generate(char charSpace[])
{
    int spaceSize = sizeof(charSpace);
    cout << charSpace << endl;
    cout << spaceSize << endl;
    return spaceSize;
}

int main(int argc, char* argv[])
{
    char space[] = "abcdefghijklmnopqrstuvwxyz";
    int anint = generate(space);
    cout<<space<<endl;
    return 0;
}


Output:

abcdefghijklmnopqrstuvwxyz
4
abcdefghijklmnopqrstuvwxyz


Shouldn't the sizeof operator return a value 27 in the function generate?
When I use the sizeof(space) in main() it returns 27. Why does it change to 4 when I do the same?
Arrays are passed to functions as pointers, so sizeof(your_array_parameter) will return the same value as sizeof(a_pointer)
That means if I need the size of the array in the other function, I'm gonna have to calculate it in the main and pass it as a second parameter, right? But that isn't too flexible. Is there another method?

And, also, isn't size of a char* equal to 1B? Why does it say 4?
Well, you could end the charSpace[] array with a 0, then you could simply loop through until you find the 0. I think this is called Null-terminated.
The best way is passing the size as parameter.
You can use std::vector instead of plain arrays
Well, you could end the charSpace[] array with a 0, then you could simply loop through until you find the 0. I think this is called Null-terminated.

I am pretty sure null-terminated means ending with a '\0' character or NULL character. Anyway, that is a good way, too. But i guess I will make do with passing the size as argument, for now.
I am pretty sure null-terminated means ending with a '\0' character or NULL character.
And that's different from 0 how?
I don't know. But they are different.
Try

1
2
3
4
5
6
7
8
9
#include <iostream>
using namespace std;

int main(void)
{
      cout<<"Zero: "<<'0'<<endl;
      cout<<"Null: "<<'\0'<<endl;
      return 0;
}

0 is printed like a zero and null character doesn't print.
Last edited on
Uh, no. You are printing the character '0' and not the character whose ascii value is 0.

Try this:
1
2
3
4
5
6
7
int main(void)
{
	unsigned char Zero = 0;
	cout<<"Zero: "<< Zero <<endl;
	cout<<"Null: "<<'\0'<<endl;
	return 0;
}
'0' is the ASCII value representing the digit 0
'\0' is the null character, a character of value 0
is the same as '\0' but of type int
Try this:
1
2
3
4
5
6
int main(void)
{
      cout<<"Zero: "<<int('0')<<endl;
      cout<<"Null: "<<int('\0')<<endl;
      return 0;
}
Another method is passing two pointers: begin and end in range [ )
Uh, no. You are printing the character '0' and not the character whose ascii value is 0.


Oh I'm sorry, I thought you were talking about the character '0'. My bad...
And yeah, @Bazzy! Always grateful to you. I always like the way you explain the things inside out. Learnt quite a few things from you.
Last edited on
Another method is passing two pointers: begin and end in range [ )

Didn't really get it. Please elaborate?
It is a common STL idiom, pass the first iterator and the one-past iterator. In your case, an iterator is a pointer. For example:

1
2
3
4
5
6
7
8
void print_int_array( int* first, int* last )
  {
  while (first != last)
    {
    cout << *first << endl;
    ++first;
    }
  }

Hope this helps.
Topic archived. No new replies allowed.