函数工作,但也返回null-C
下面的代码旨在获取URL路径并将其转换为小写。它完成了任务,但它也在小写路径名后吐出(null)。据我所知,这与我的函数工作,但也返回null-C,c,arrays,string,C,Arrays,String,下面的代码旨在获取URL路径并将其转换为小写。它完成了任务,但它也在小写路径名后吐出(null)。据我所知,这与我的temp数组需要空终止符有关。我已经为它腾出了空间并尝试分配它,但我得到以下错误可变大小的对象可能未初始化。我不完全确定如何解决这个问题,因为我还没有培养在char*和数组表示法之间进行完全舒适的切换。如果有人能给我指出正确的方向,我将不胜感激 #include <stdio.h> #include <stdlib.h> #inclu
temp
数组需要空终止符有关。我已经为它腾出了空间并尝试分配它,但我得到以下错误可变大小的对象可能未初始化。我不完全确定如何解决这个问题,因为我还没有培养在char*和数组表示法之间进行完全舒适的切换。如果有人能给我指出正确的方向,我将不胜感激
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <strings.h>
#include <string.h>
const char* lookup(const char* path);
int main (void)
{
const char* test = lookup("http://WWW.google.COM");
printf("%s", test);
return 0;
}
const char* lookup(const char* path)
{
// this is where I tried to add the null terminator
char temp[strlen(path) + 1];
strcpy(temp, path);
for (int i = 0, n = strlen(path); i < n; i++)
{
if (isalpha(temp[i]))
{
if (isupper(temp[i]))
{
temp[i] = tolower(temp[i]);
}
}
}
printf("%s", temp);
printf("\n");
return 0;
}
#包括
#包括
#包括
#包括
#包括
常量字符*查找(常量字符*路径);
内部主(空)
{
常量字符*测试=查找(“http://WWW.google.COM");
printf(“%s”,测试);
返回0;
}
常量字符*查找(常量字符*路径)
{
//这就是我试图添加空终止符的地方
字符温度[strlen(路径)+1];
strcpy(温度、路径);
for(int i=0,n=strlen(路径);i
数组大小(在本例中为char
数组)需要在编译时定义。您正试图在运行时在lookup()
函数中定义它
有两种解决方法:
- 使用
char
指针和malloc()
代替固定数组:
char* temp = malloc (sizeof(path) + 1);
- 声明具有固定大小的数组(
char temp[100]
),但请记住,输入字符串(+1表示空终止)不能超过此长度
使用第一个选项的完整解决方案,包括其他海报指出的修复,并删除一些冗余,如下所示:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <strings.h>
#include <string.h>
const char* lookup(const char* path);
int main (void)
{
const char* test = lookup("http://WWW.google.COM");
printf("%s", test);
printf("\n");
free((void*)test);
return 0;
}
const char* lookup(const char* path)
{
// this is where I tried to add the null terminator
char* temp = malloc (strlen(path) + 1);
strcpy(temp, path);
for (int i = 0, n = strlen(path); i < n; i++)
{
if (isupper(temp[i]))
{
temp[i] = tolower(temp[i]);
}
}
return temp;
}
#包括
#包括
#包括
#包括
#包括
常量字符*查找(常量字符*路径);
内部主(空)
{
常量字符*测试=查找(“http://WWW.google.COM");
printf(“%s”,测试);
printf(“\n”);
自由((空隙*)试验);
返回0;
}
常量字符*查找(常量字符*路径)
{
//这就是我试图添加空终止符的地方
char*temp=malloc(strlen(路径)+1);
strcpy(温度、路径);
for(int i=0,n=strlen(路径);i
数组大小(在本例中为char
数组)需要在编译时定义。您正试图在运行时在lookup()
函数中定义它
有两种解决方法:
- 使用
char
指针和malloc()
代替固定数组:
char* temp = malloc (sizeof(path) + 1);
- 声明具有固定大小的数组(
char temp[100]
),但请记住,输入字符串(+1表示空终止)不能超过此长度
使用第一个选项的完整解决方案,包括其他海报指出的修复,并删除一些冗余,如下所示:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <strings.h>
#include <string.h>
const char* lookup(const char* path);
int main (void)
{
const char* test = lookup("http://WWW.google.COM");
printf("%s", test);
printf("\n");
free((void*)test);
return 0;
}
const char* lookup(const char* path)
{
// this is where I tried to add the null terminator
char* temp = malloc (strlen(path) + 1);
strcpy(temp, path);
for (int i = 0, n = strlen(path); i < n; i++)
{
if (isupper(temp[i]))
{
temp[i] = tolower(temp[i]);
}
}
return temp;
}
#包括
#包括
#包括
#包括
#包括
常量字符*查找(常量字符*路径);
内部主(空)
{
常量字符*测试=查找(“http://WWW.google.COM");
printf(“%s”,测试);
printf(“\n”);
自由((空隙*)试验);
返回0;
}
常量字符*查找(常量字符*路径)
{
//这就是我试图添加空终止符的地方
char*temp=malloc(strlen(路径)+1);
strcpy(温度、路径);
for(int i=0,n=strlen(路径);i
您的查找
函数始终返回0(并且返回0;
应该写入返回NULL;
,以确保可读性)。顺便说一句,它不能返回本地数组(也就是说,代码中的returntemp;
可能是一些)。您希望它返回一个指向堆分配指针的指针(可能使用malloc
或strdup
),然后您需要一个关于free
绑定该区域的约定。您可以(在一些注释中)记录lookup
正在返回一个堆分配的指针,并且调用方通常负责free
-ing它
阅读更多关于
不要忘记用所有警告和调试信息编译代码(例如,gcc-Wall-g
)使用调试器(gdb
)&。您的查找
函数始终返回0(并且返回0;
应写入返回NULL;
,以确保可读性)。顺便说一句,它不能返回本地数组(也就是说,代码中的returntemp;
可能是一些)。您希望它返回一个指向堆分配指针的指针(可能使用malloc
或strdup
),然后您需要一个关于free
绑定该区域的约定。您可以(在一些注释中)记录lookup
正在返回一个堆分配的指针,并且调用方通常负责free
-ing它
阅读更多关于
不要忘记用所有警告和调试信息编译代码(例如,gcc-Wall-g
)使用调试器(gdb
)&查找函数返回0
。我相信你想返回temp
@vkgade——绝对是。非常感谢isupper
始终是isalpha
。。。如果不需要检查的话…@anishsane-太好了。非常感谢。将替换为单个isupper()if语句,以将大写字符转换为小写字符。对收紧措施的暗示总是值得赞赏的