我试图将一些参数传递给一个函数,将这些值存储到结构中的一组元素中。然后通过调用另一个函数从结构中打印这些值。
这是我正在尝试做的事情:
#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"
}
printTest
函数似乎没有打印出任何内容,如果我在其中硬核 printf,它会打印空值和零。但是,我尝试在 test
函数中打印结构值,该函数在初始化值后确实有效。
例如,如果我执行 printf("%d",mem->num);
这会打印出 200 的值(在 test
函数中) .
但是最后一个函数中的 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;
}
关于c - 函数中的返回语句从不打印到屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29324650/