[英]Failing to pass a string through a function in c
我正在学习 c 并使用字符串、函数和指针进行一些练习。 练习是将一个字符串带入一个函数中,进行阐述并给出结果。 函数 'LenCorr' 在输入字符串的末尾添加 'i' 字母以实现 8 个字符的长度。
我的代码是:
#include <stdio.h>
#include <string.h>
#define StrLen 100
char * LenCorr(char * str);
void main(void)
{
char t1[StrLen] = "Hello3"; // this is the input string
printf("Input : %s\n", t1);
char *t3 = LenCorr(t1); // pass through the 'LenCorr' function to t3 pointer to char array
printf("LenCorr p: %p\n", t3); // then I print the pointer ...
printf("LenCorr s: %s\n", t3); // ... but the content is empty
}
char * LenCorr(char * str)
{
char si[StrLen]; // I create various strings
char su[StrLen];
char st[StrLen] = "iiiiiiiiiiiiiiiiiii";
strcpy(si,str); // I have to copy the 'str' input to the local string
// here I perform some elaborations
int ln = strlen(si);
int ct = 8 - ln;
if (ln < 8) {strncat(si, st, ct);}
// ... and the final result is correct into 'si' string
strcpy(su,si); // that I copy into a 'su' string (another)
char * output = su;
printf("Debug output: %s\n", output); // here I see the result of string elaboration
printf("Debug output pointer: %p\n", output); // here I get a pointer of the string elaboration
return output;
}
但最后函数返回正确的指针值但值为 0。这里是控制台输出:
Input : Hello3
Debug output: Hello3hh
Debug output ptr: 0x7fff4cf17c30
LenCorr p: 0x7fff4cf17c30
LenCorr s:
为什么 ?
正如我在评论中提到的,您只需要使用 malloc 在堆上分配空间,以便在函数结束(从堆栈中弹出)时它不会被破坏。 只需确保在 main.js 的末尾使用free()
。
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#define StrLen 100
char * LenCorr(char * str);
int main(void)
{
char t1[StrLen] = "Hello3\0";
printf("Input : %s\n", t1);
char *t3 = LenCorr(t1);
printf("LenCorr p: %p\n", t3);
printf("LenCorr s: %s\n", t3);
free(t3);
}
char * LenCorr(char * str)
{
char si[StrLen];
char st[StrLen] = "iiiiiiiiiiiiiiiiiii\0";
strncpy(si,str, strlen(str));
int ln = strlen(si);
int ct = 8 - ln;
if (ln < 8) { strncat(si, st, ct); }
char * output = malloc(strlen(si) * sizeof(char)); //heap-allocation
strncpy(output, si, strlen(si));
printf("Debug output: %s\n", output);
printf("Debug output pointer: %p\n", output);
return output;
}
char
数组的生命周期仅限于它声明的范围,一旦函数执行结束,访问本地数组就不再合法。
这正是这里发生的情况, output
将在返回时指向无效的内存位置,稍后像您在main
所做的那样访问该内存位置会导致未定义的行为。
执行此操作的惯用方法是将指向缓冲区的指针作为函数的参数传递给您要存储字符串的缓冲区:
#include <stdio.h>
#include <string.h>
#define StrLen 100
void LenCorr(char *str, char* output); // passing a pointer to the destination bufffer
void main(void)
{
char t1[StrLen] = "Hello3";
char t3[StrLen]; // destination buffer
printf("Input : %s\n", t1);
LenCorr(t1, t3); // pass destination buffer as argument
printf("LenCorr p: %p\n", t3);
printf("LenCorr s: %s\n", t3);
}
void LenCorr(char * str, char *output)
{
char si[StrLen];
char su[StrLen];
char st[StrLen] = "iiiiiiiiiiiiiiiiiii";
strcpy(si,str);
int ln = strlen(si);
int ct = 8 - ln;
if (ln < 8) {strncat(si, st, ct);}
strcpy(su, si);
strcpy(output, su); // copy string to its final destination
printf("Debug output: %s\n", output);
printf("Debug output pointer: %p\n", output);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.