字符串在没有赋值的情况下连接到另一个字符串,这是为什么?

发布于 2024-11-09 08:35:35 字数 1111 浏览 0 评论 0原文

下面是程序中的一个函数:

//read the specified file and check for the input ssn
int readfile(FILE *fptr, PERSON **rptr){
    int v=0, i, j;
    char n2[MAXS+1], b[1]=" ";

    for(i=0; i<MAXR; i++){
        j=i;
        if(fscanf(fptr, "%c\n%d\n%19s %19s\n%d\n%19s\n%d\n%19s\n%19s\n%d\n%d\n%19s\n\n",
                    &rptr[j]->gender, &rptr[j]->ssn, rptr[j]->name, n2, &rptr[j]->age, 
                    rptr[j]->job, &rptr[j]->income, rptr[j]->major, rptr[j]->minor,
                    &rptr[j]->height, &rptr[j]->weight, rptr[j]->religion)==EOF) {
            i=MAXR;
        }
        strcat(rptr[j]->name, b);
        //strcat(rptr[j]->name, n2);
        if(&rptr[MAXR]->ssn==&rptr[j]->ssn)
            v=j;
    }
    return v;
}

注释行就像这样,因为由于某种原因数组“b”包含字符串“n2”,尽管明显缺少赋值。这发生在第一次 strcat 调用之前,但在 fscanf 调用之后/期间。

它确实实现了预期的目标,但为什么 n2 连接到 b 的末尾,特别是当 b 只为 1 个数组元素保留空间时?

以下是 fscanf 调用后的变量定义片段:

*rptr[j]->name = "Rob"

b = " Low"

n2= "Low"

Below is a function from a program:

//read the specified file and check for the input ssn
int readfile(FILE *fptr, PERSON **rptr){
    int v=0, i, j;
    char n2[MAXS+1], b[1]=" ";

    for(i=0; i<MAXR; i++){
        j=i;
        if(fscanf(fptr, "%c\n%d\n%19s %19s\n%d\n%19s\n%d\n%19s\n%19s\n%d\n%d\n%19s\n\n",
                    &rptr[j]->gender, &rptr[j]->ssn, rptr[j]->name, n2, &rptr[j]->age, 
                    rptr[j]->job, &rptr[j]->income, rptr[j]->major, rptr[j]->minor,
                    &rptr[j]->height, &rptr[j]->weight, rptr[j]->religion)==EOF) {
            i=MAXR;
        }
        strcat(rptr[j]->name, b);
        //strcat(rptr[j]->name, n2);
        if(&rptr[MAXR]->ssn==&rptr[j]->ssn)
            v=j;
    }
    return v;
}

the commented line is like that because for some reason the array 'b' contains the string 'n2' despite an obvious lack of an assignment. This occurs before the first strcat call, but after/during the fscanf call.

it does accomplish the desired goal, but why is n2 concatenated onto the end of b, especially when b only has reserved space for 1 array element?

Here is a snippet of variable definitions after the fscanf call:

*rptr[j]->name = "Rob"

b = " Low"

n2= "Low"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无所谓啦 2024-11-16 08:35:35

它有效,因为你很幸运。 bn2 在内存中恰好以正确的顺序相邻。 C 不对数组进行边界检查,并且很乐意让您溢出它们。因此,您可以这样声明一个数组:

char someArray[1] = "lots and lots of characters";

C 编译器(当然是旧的编译器)会认为这没问题,即使 someArray 中显然没有足够的空间来存储那么多字符。我不确定它是否定义了在这种情况下会做什么(我怀疑没有),但在我的编译器上,它将人口限制为数组的大小,因此它不会溢出边界(someArray =={'l'})。

您的情况是相同的(尽管不那么极端)。 char b[1] 正在创建一个有足够空间存储 1 个字节的数组。您在该字节中放置了一个空格,因此没有空间容纳空终止符。 strcat,不断复制内存,直到到达空终止符,因此它将继续下去,直到找到一个终止符,即使直到下一个字符串的末尾(这就是您的情况发生的情况) )。

如果您使用的是 C++ 编译器,它至少会抛出一个警告(或更可能是一个错误),告诉您您试图将太多项放入数组中。

It works, because you got lucky. b and n2 happened to be next to each other in memory, in the right order. C doesn't do boundary checking on arrays and will quite happily let you overflow them. So you can declare an array like this:

char someArray[1] = "lots and lots of characters";

The C compiler (certainly old ones) is going to think this is fine, even though there clearly isn't enough space in the someArray to store that many characters. I'm not sure if it's defined what it'll do in this situation (I suspect not), but on my compiler it limits the population to the size of the array, so it doesn't overflow the boundary (someArray=={'l'}).

Your situation is the same (although less extreme). char b[1] is creating an array with enough room to store 1 byte. You're putting a space in that byte, so there's no room for the null terminator. strcat, keeps copying memory until it gets to a null terminator, consequently it'll keep going until it finds one, even if it's not until the end of the next string (which is what's happening in your case).

If you had been using a C++ compiler, it would have thrown at least a warning (or more likely an error) to tell you that you were trying to put too many items into the array.

雨落星ぅ辰 2024-11-16 08:35:35

B 的大小需要为 2,1 为空格,1 为空。

B needs to be size 2, 1 for the space, 1 for the null.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文