gpt4 book ai didi

c - 将数据传递给函数时遇到问题

转载 作者:行者123 更新时间:2023-11-30 19:03:08 25 4
gpt4 key购买 nike

用 C 语言编写程序,我试图将两个变量传递到函数 kstrextend 中。 Name 是一个单词或一组字符,存储在值 kstring 中,而 a 是一个数值,但据我所知, name 根本没有传递到函数中,而且我不明白为什么。是否有东西没有正确存储?因为该函数工作正常,所以我只是无法正确传递名称。

kstring和名称声明:

kstring name;
char kstring[50];

类型定义:

typedef struct
{
char *data;
size_t length;
} kstring;

功能:

void kstrextend(kstring *strp, size_t nbytes)
{
char *nwData;
int lnth=strp->length;
if(lnth < nbytes)
{
// new array allocate with large size and copy data to new array
nwData = (char *)realloc(strp->data, nbytes);
// call abort in case of error
if(nwData == NULL)
{
abort();
}
//Making strp->data point to the new array
strp->data = nwData;
//Setting strp->length to the new size.
strp->length = nbytes;
for(int i = 0; i <= lnth; i++)
{
printf("\n %s",strp->data);
}
// filled with '\0' in remaining space of new array
for (int lp = lnth; lp < nbytes; lp++)
{
strp->data[lp] = '\0';
printf("\n %s", strp->data[lp]);
}
}
}

主要部分:

    size_t a;
char * k = kstring;
printf("\n Enter number: ");
scanf("%d", &a);
name.data = (char*)calloc(sizeof(k), 1);
strcpy(input, k);
name.length= kstring_length;
kstrextend(&name,a);

最佳答案

首先,您的变量名称具有误导性kstring 。使用其他类似 kstring_init 的内容并为其赋值。我假设您想初始化 name类型变量 kstring与某些东西,然后改变它的长度。这就是全部内容。然后定义一个char *类型的常量并用它初始化 kstring 的长度和数据。然后使用realloc使用输入值 a 来扩展指针的内存,而不是 k 的大小。那没有意义。自尺寸k是指针的大小,它是常数。

在你的函数中:不要使用int如果您通过size_t 。使用相同的数据类型来执行相同的操作。

0 的循环中至lnth ,您输出相同的字符串 lnth+1次,这没有意义。您可能想输出字符串的字符。所以使用 %c并使用字符数组的索引并且不设置 <= lnth但是< lnth作为上限。如果有符号和无符号,请注意数据类型!

设计提示:如果您有一个 if block ,它会包装所有代码...反转条件并退出,以便代码位于 if block 之后。

size_t 一起工作时要小心和int ,自 int已签名且 size_t不是,这可能会在 if 语句中产生问题。

不要使用abort而是exit 。您不希望您的程序异常中止并进行核心转储。

您的程序的工作版本是:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

typedef struct
{
char *data;
size_t length;
} kstring;

kstring name;
char *kstring_init = "blabla";

void kstrextend(kstring *strp, size_t nbytes)
{
char *nwData;

size_t lnth = strp->length;

if ((int) lnth >= (int) nbytes) {
printf("Error, size already larger than requested size.\n");
exit(-1);
}

// new array allocate with large size and copy data to new array
nwData = realloc(strp->data, sizeof(char) * (int) nbytes);
if(nwData == NULL)
{
printf("Error, realloc returned NULL\n");
exit(-1);
}

//Making strp->data point to the new array
strp->data = nwData;
//Setting strp->length to the new size.
strp->length = nbytes;

for(int i = 0; i < lnth; i++)
{
printf("\n %c", strp->data[i]);
}
// filled with '\0' in remaining space of new array
for (int lp = lnth; lp < (int) nbytes; lp++)
{
strp->data[lp] = '\0';
printf("\n %c", strp->data[lp]);
}
}

int main(void)
{
size_t a;

printf("\n Enter number: ");
scanf("%d", &a);

name.length = strlen(kstring_init) + 1;
printf("Length of string is: %d\n", name.length);
name.data = (char*)malloc(sizeof(char) * name.length);
strcpy(name.data, kstring_init);

printf("Old string: %s\n", name.data);
printf("You want to reallocate %d bytes\n", a);

kstrextend(&name, a);

return 0;
}

关于c - 将数据传递给函数时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54529015/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com