C 函数中的Return语句从不打印到屏幕
我试图将一些参数传递到一个函数中,将这些值存储到结构中的一组元素中。然后通过调用另一个函数从结构中打印这些值 以下是我想做的:C 函数中的Return语句从不打印到屏幕,c,pointers,memory,struct,C,Pointers,Memory,Struct,我试图将一些参数传递到一个函数中,将这些值存储到结构中的一组元素中。然后通过调用另一个函数从结构中打印这些值 以下是我想做的: #include <stdio.h> #include <string.h> #include <stdlib.h> typedef struct temp { int num; char * name; struct temp * nextPtr; }temp; temp * test(); char *
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct temp
{
int num;
char * name;
struct temp * nextPtr;
}temp;
temp * test();
char * printTest(temp * print);
int main (void)
{
test("TV",200);
struct temp * t;
printTest(t);
return 0;
}
temp * test(char * name, int num)
{
struct temp * mem = malloc(sizeof(struct temp));
mem->name = malloc(sizeof(strlen(name) + 1));
mem->name = name;
mem->num = num;
mem->nextPtr = NULL;
return mem;
}
char * printTest(temp * print)
{
char * output;
output = malloc(sizeof(struct temp));
print = malloc(sizeof(struct temp));
sprintf(output,"It's name is %s, and it costs %d",print->name,print->num);
return output; //should print "TV" and costs "200"
}
#包括
#包括
#包括
类型定义结构温度
{
int-num;
字符*名称;
结构温度*nextPtr;
}温度;
温度*测试();
字符*打印测试(临时*打印);
内部主(空)
{
测试(“电视”,200);
结构温度*t;
打印测试(t);
返回0;
}
临时*测试(字符*名称,整数)
{
结构温度*mem=malloc(sizeof(结构温度));
mem->name=malloc(sizeof(strlen(name)+1));
mem->name=name;
mem->num=num;
mem->nextPtr=NULL;
返回mem;
}
字符*打印测试(临时*打印)
{
字符*输出;
输出=malloc(sizeof(struct temp));
print=malloc(sizeof(struct temp));
sprintf(输出,“名称为%s,成本为%d”,打印->名称,打印->数量);
返回输出;//应打印“TV”,成本为“200”
}
函数printTest
,似乎没有打印出任何内容,相反,如果我在其中使用hardcoreprintf,它会打印空值和零。但是,我尝试在test
函数中打印struct值,该函数在初始化值后确实起作用
例如,如果我执行printf(“%d”,mem->num)代码>这会打印出200的值(在测试功能中)
但是最后一个函数中的sprintf和return组合不会产生相同的结果。任何帮助都将不胜感激 您没有捕获测试返回的值,因此它会丢失:
int main (void)
{
//changed to capture return value of test.
struct temp * t = test("TV",200);
printTest(t);
return 0;
}
此外,您的打印功能错误:
// this now points to the struct you allocated in test.
char * printTest(temp * print)
{
char * output;
// you don't really want the size of temp here, you want the size
// of your formatted string with enough room for all members of the
// struct pointed to by temp.
output = malloc(sizeof(struct temp));
// you're not using this.
print = malloc(sizeof(struct temp));
// you sprintf from a buffer pointing to nothing, into your output buffer
// writing past the memory you actually allocated.
sprintf(output,"It's name is %s, and it costs %d",print->name,print->num);
// return doesn't print anything, it simply returns the value that your
// function signature specifies, in this case char *
return output; //should print "TV" and costs "200"
}
尝试此操作,您将获取分配的指针,并使用printf
将格式化字符串写入标准输出:
// we're not returning anything
void printTest(temp * print ){
if (temp == NULL ){
// nothing to print, just leave.
return;
}
printf("It's name is %s, and it costs %d",print->name,print->num);
return;
}
还有一些关于测试函数的注释:
// char is a pointer to a string, from your invocation in main, this is
// a string stored in static read only memory
temp * test(char * name, int num)
{
// you malloc memory for your struct, all good.
struct temp * mem = malloc(sizeof(struct temp));
// you malloc space for the length of the string you want to store.
mem->name = malloc(sizeof(strlen(name) + 1));
// here's a bit of an issue, mem->name is a pointer, and name is a pointer.
// what you're doing here is assigning the pointer name to the variable
// mem->name, but you're NOT actually copying the string - since you
// invoke this method with a static string, nothing will happen and
// to the string passed in, and you'll be able to reference it - but
// you just leaked memory that you allocated for mem->name above.
mem->name = name;
// num is not apointer, it's just a value, therefore it's okay to assign
// like this.
mem->num = num;
mem->nextPtr = NULL;
return mem;
}
请尝试以下方法:
temp * test(char * name, int num)
{
struct temp * mem = malloc(sizeof(struct temp));
// we still malloc room for name, storing the pointer returned by malloc
// in mem->name
mem->name = malloc(sizeof(strlen(name) + 1));
// Now, however, we're going to *copy* the string stored in the memory location
// pointed to by char * name, into the memory location we just allocated,
// pointed to by mem->name
strcpy(mem->name, name);
mem->num = num;
mem->nextPtr = NULL;
return mem;
}
您没有捕获测试返回的值,因此它会丢失:
int main (void)
{
//changed to capture return value of test.
struct temp * t = test("TV",200);
printTest(t);
return 0;
}
此外,您的打印功能错误:
// this now points to the struct you allocated in test.
char * printTest(temp * print)
{
char * output;
// you don't really want the size of temp here, you want the size
// of your formatted string with enough room for all members of the
// struct pointed to by temp.
output = malloc(sizeof(struct temp));
// you're not using this.
print = malloc(sizeof(struct temp));
// you sprintf from a buffer pointing to nothing, into your output buffer
// writing past the memory you actually allocated.
sprintf(output,"It's name is %s, and it costs %d",print->name,print->num);
// return doesn't print anything, it simply returns the value that your
// function signature specifies, in this case char *
return output; //should print "TV" and costs "200"
}
尝试此操作,您将获取分配的指针,并使用printf
将格式化字符串写入标准输出:
// we're not returning anything
void printTest(temp * print ){
if (temp == NULL ){
// nothing to print, just leave.
return;
}
printf("It's name is %s, and it costs %d",print->name,print->num);
return;
}
还有一些关于测试函数的注释:
// char is a pointer to a string, from your invocation in main, this is
// a string stored in static read only memory
temp * test(char * name, int num)
{
// you malloc memory for your struct, all good.
struct temp * mem = malloc(sizeof(struct temp));
// you malloc space for the length of the string you want to store.
mem->name = malloc(sizeof(strlen(name) + 1));
// here's a bit of an issue, mem->name is a pointer, and name is a pointer.
// what you're doing here is assigning the pointer name to the variable
// mem->name, but you're NOT actually copying the string - since you
// invoke this method with a static string, nothing will happen and
// to the string passed in, and you'll be able to reference it - but
// you just leaked memory that you allocated for mem->name above.
mem->name = name;
// num is not apointer, it's just a value, therefore it's okay to assign
// like this.
mem->num = num;
mem->nextPtr = NULL;
return mem;
}
请尝试以下方法:
temp * test(char * name, int num)
{
struct temp * mem = malloc(sizeof(struct temp));
// we still malloc room for name, storing the pointer returned by malloc
// in mem->name
mem->name = malloc(sizeof(strlen(name) + 1));
// Now, however, we're going to *copy* the string stored in the memory location
// pointed to by char * name, into the memory location we just allocated,
// pointed to by mem->name
strcpy(mem->name, name);
mem->num = num;
mem->nextPtr = NULL;
return mem;
}
另外,sprintf
输出到字符串。它不输出到标准输出,即打印到屏幕,不像printf
那样。您可能需要调用printf
或put
到输出
字符串 另外,sprintf
输出到字符串。它不输出到标准输出,即打印到屏幕,不像printf
那样。您可能需要调用printf
或put
到输出
字符串 问题比乍一看要多得多。这是一个清理过的版本。不能分配字符串(例如mem->name=name;
)。您可以为mem->name
分配,然后为其分配strncpy
名称,或者使用strdup
立即分配和复制。在printest
中,您必须为输出分配足够的空间,以容纳格式字符串的静态部分加上print->name
和print->num
。请查看以下内容,如果您有问题,请告诉我
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct temp
{
int num;
char * name;
struct temp * nextPtr;
}temp;
temp * test(char * name, int num);
char * printTest(temp * print);
int main (void)
{
struct temp * t = test("TV",200);
// struct temp * t;
printf ("%s\n", printTest(t));
return 0;
}
temp * test(char * name, int num)
{
struct temp * mem = malloc(sizeof(struct temp));
// mem->name = malloc(sizeof(strlen(name) + 1));
// mem->name = name;
mem->name = strdup (name);
mem->num = num;
mem->nextPtr = NULL;
return mem;
}
char * printTest(temp * print)
{
char *output = NULL;
// output = malloc(sizeof(struct temp));
// print = malloc(sizeof(struct temp));
output = malloc (strlen (print->name) + sizeof print->num + 30);
sprintf(output,"It's name is %s, and it costs %d",print->name,print->num);
return output; //should print "TV" and costs "200"
}
问题比乍一看要多得多。这是一个清理过的版本。不能分配字符串(例如mem->name=name;
)。您可以为mem->name
分配,然后为其分配strncpy
名称,或者使用strdup
立即分配和复制。在printest
中,您必须为输出分配足够的空间,以容纳格式字符串的静态部分加上print->name
和print->num
。请查看以下内容,如果您有问题,请告诉我
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct temp
{
int num;
char * name;
struct temp * nextPtr;
}temp;
temp * test(char * name, int num);
char * printTest(temp * print);
int main (void)
{
struct temp * t = test("TV",200);
// struct temp * t;
printf ("%s\n", printTest(t));
return 0;
}
temp * test(char * name, int num)
{
struct temp * mem = malloc(sizeof(struct temp));
// mem->name = malloc(sizeof(strlen(name) + 1));
// mem->name = name;
mem->name = strdup (name);
mem->num = num;
mem->nextPtr = NULL;
return mem;
}
char * printTest(temp * print)
{
char *output = NULL;
// output = malloc(sizeof(struct temp));
// print = malloc(sizeof(struct temp));
output = malloc (strlen (print->name) + sizeof print->num + 30);
sprintf(output,"It's name is %s, and it costs %d",print->name,print->num);
return output; //should print "TV" and costs "200"
}
这行:“sprintf(输出,“它的名称是%s,成本是%d”,print->name,print->num)”需要一些重大修改。1) 源字段“print->name和print->num在分配的内存段内,但它们从未设置为任何特定值,因此它们包含垃圾。目标指针“output”指向分配的内存区域,但该区域不大于源区域,(通常,它被格式化为“temp”结构,而不是一个长字符串。由于它只有temp结构的大小,sprintf format参数中没有多余字符的空间。结果将是未定义的行为,因为sprintf的输出将超出分配的内存区域。这一行:'sprintf(输出,“其名称为%s,成本为%d”,打印->名称,打印->数量)”需要一些重大修改。1)源字段“print->name和print->num位于分配的内存段内,但它们从未设置为任何特定值,因此它们包含垃圾。目标指针“output”指向分配的内存区域,但该区域不大于源区域,(通常,它被格式化为一个“temp”结构,而不是一个长字符串。由于它只是一个temp结构的大小,sprintf format参数中没有多余字符的空间。由于sprintf的输出将超出分配的内存区域,因此结果将是未定义的行为*t=test(“TV”,200);
您从未使用过返回值。这一行:“mem->name=name;”正在移动指针,而不是复制字符串。要复制字符串,请在调用malloc函数(和族)时使用strcpy(mem->name,name)”,始终检查返回值(!=NULL),以确保操作成功struct temp*t=test(“TV”,200);
您从未使用过返回值。此行:“”mem->name=name;;正在移动指针,而不是复制字符串。要复制字符串,请使用strcpy(mem->name,nam)