Why doesn't the C/C++ compiler give an error even if you put b[4] in a function that takes a[1] as a factor?

Asked 2 years ago, Updated 2 years ago, 43 views

#include<iostream>
using namespace std;
int dis(char a[1])
{
    int length = strlen(a);
    char c = a[2];
    return length;
}
int main()
{
    char b[4] = "abc";
    int c = dis(b);
    cout << c;
    return 0;
}

I'm asking you because 1 seems to do nothing for int dis(chara[1]).

When you actually write a function, it doesn't matter if the array length is 4 or 5 Then, what's the point of designating it as "1" from the compiler's point of view?

c c++ array

2022-09-21 19:24

1 Answers

int dis(chara[1]) A code that you write seems to carry an array, but it's not really an array Passes the address of the first element that the pointer points to.

The value in [] is ignored because the pointer does not know how long the array is.

It's kind of an expedient


2022-09-21 19:24

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.