Alex G
Alex G

Reputation: 747

C++ Array Pass to Function gets cut off

I am new to C/C++ and am learning about arrays. I just finished reading about passing arrays to functions, and am trying to implement it. I start off with an array x[] = "Hello, which of course has a sizeof() 6. But for some reason, when I pass it to my function, listElements(char arr[]), then I get sizeof(arr) equal to 4. Why is my array being cut off like this?

Also, sizeof(char) on my compiler is 1.

#include <iostream>
using namespace std;

void listElements(char[]);

int main(){
    char x[] = "Hello";
    cout << sizeof(x) << endl; // 6
    listElements(x);
    cin >> x[0];
    return 0;
}

void listElements(char arr[]){
    cout << "Size of arr: " << sizeof(arr) << endl; // 4  <--- why is this 4?
    cout << "Size of char: " << sizeof(char) << endl; // 1
    for (int j = 0; j < (sizeof(arr) / sizeof(char)); j++){
        cout << arr[j] << endl;
    }
    return;
}

Upvotes: 0

Views: 1180

Answers (4)

vsoftco
vsoftco

Reputation: 56577

If you really want to pass an array, then pass it by reference (or const reference if you don't intend to change it) via a template, like

template<std::size_t N>
void listElements(/*const*/ char (&arr)[N])
{ 
    // sizeof(arr) will be equal to N
    // rest of the function body here
}

If you pass it by value, then it decays to a pointer, i.e. void listElements(char arr[]) is the same as void listElements(charr arr[1024]) is the same as void listElements(char* arr).

PS: note that if you pass the array by reference like above, then your function won't work with pointers anymore, as in this case the compiler differentiate between pointers and arrays. That is, listElements(p) won't compile for char* p;. If you want to use it with pointers (which may be convenient), then you must specify the size manually as an additional parameter of the function, or use some convention for an end-of-array delimiter, such as a zero-terminated strings in C.

Upvotes: 8

Tony J
Tony J

Reputation: 651

char x[] = "Hello";
cout << sizeof(x) << endl; // 6 because the compiler knows x is an array of 6 characters long

void listElements(char arr[]){
    cout << "Size of arr: " << sizeof(arr) << endl; // 4  arr[] is actually a pointer

Calling listElement() is doesn't actually pass the entire array, it only passes the pointer to x. I think that's why you expected 6.

Upvotes: 3

RobStone
RobStone

Reputation: 187

You are getting the size of a 32bit pointer to a null terminated char[]. Effectively, a char*

Upvotes: 1

Baum mit Augen
Baum mit Augen

Reputation: 50111

char[] as a function argument is the same as char* in C++. And apparently on your system, sizeof(char*) == 4. Also, sizeof(char) always equals 1.

To properly pass the C-style string, pass its length as extra integer argument or, even better, use std::string instead of char[] and char*.

Code examples:

void listElements(char *arr, int length){
    for (int j = 0; j < length; j++){
        cout << arr[j] << endl;
    }
}

or

void listElements(const std::string& str){
    for (const auto& v : str)
        std::cout << v << std::endl;
}

Upvotes: 5

Related Questions