如何将 void 指针类型转换为 C 中的字符串?
How to typecast a void pointer into a string in C?
我正在使用通用链表为通用堆栈编写程序。基本上,每个 currNd -> 数据都是 void* 数据。所以我使用 sprintf 将数据实际格式化为我的 str 字符串变量。但问题是当 currNd 是整数或实数时,它不起作用。是否有任何建议可以将 double、float、int 转换为字符串变量,以便我可以在我的 toString 函数中使用它?
这是插入代码
char *name = "Alex";
int id = 23219434;
int age = 24;
double mark = 89.5;
char grade = 'A';
push( stack, name );
push( stack, &id );
push( stack, &age );
push( stack, &mark );
push( stack, &grade );
printf("%s\n", toString(stack)); /* Output is only Alex (some strange symbols) and A */
我的 toString
char* toString(LinkedList *list)
{
char *str = (char*) malloc(sizeof(char) * STR_LENGTH);
/* Traversal starts from head */
LinkedListNode *currNd = list -> head;
/* Store the string as traversing the linked list */
while( currNd != NULL )
{
/* Format all data to be string */
sprintf(str, "%s ", (char*)(currNd -> data));
printf("%s\n", str);
currNd = currNd -> next; /* Move to the next node */
}
return str;
}
可读的输出只有姓名和年级。其他数值变量只是 return 一些奇怪的符号。希望问题清楚,谢谢!
这段代码没有多大意义:
/* Format all data to be string */
sprintf(str, "%s ", (char*)(currNd -> data)); (1)
printf("%s\n", str); (2)
因为(1)表示currNd->data其实是一个字符串。
如果 currNd->data 是字符串,则不需要将其转换为临时(malloced)字符串。
这一行就足够了:
printf("%s\n", currNd -> data);
关于代码我们不能说太多,因为我们不知道 push
和 LinkedList
。但我假设 push
需要做两件不同的事情。如果参数是string
,它应该存储为string
。如果参数是 integer
它应该将其存储为 integer
.
因此,函数 push
似乎缺少枚举类型参数。
push( stack, name, TT_TYPE_STRING ); // will store the string
push( stack, id, TT_TYPE_INT ); // will store the integer
push( stack, age, TT_TYPE_INT ); // will store the integer
关于函数 toString()
它应该检查项目的类型。如果是字符串,前面的printf("%s", ...)
应该可以,如果是整数,应该调用printf(%d", ...)
.
所以我希望你的数据结构遗漏了一个重要的点:
struct ListItem
{
enum ItemType; /* INT OR String OR Something */
int IntValue;
char *StringValue;
};
我正在使用通用链表为通用堆栈编写程序。基本上,每个 currNd -> 数据都是 void* 数据。所以我使用 sprintf 将数据实际格式化为我的 str 字符串变量。但问题是当 currNd 是整数或实数时,它不起作用。是否有任何建议可以将 double、float、int 转换为字符串变量,以便我可以在我的 toString 函数中使用它?
这是插入代码
char *name = "Alex";
int id = 23219434;
int age = 24;
double mark = 89.5;
char grade = 'A';
push( stack, name );
push( stack, &id );
push( stack, &age );
push( stack, &mark );
push( stack, &grade );
printf("%s\n", toString(stack)); /* Output is only Alex (some strange symbols) and A */
我的 toString
char* toString(LinkedList *list)
{
char *str = (char*) malloc(sizeof(char) * STR_LENGTH);
/* Traversal starts from head */
LinkedListNode *currNd = list -> head;
/* Store the string as traversing the linked list */
while( currNd != NULL )
{
/* Format all data to be string */
sprintf(str, "%s ", (char*)(currNd -> data));
printf("%s\n", str);
currNd = currNd -> next; /* Move to the next node */
}
return str;
}
可读的输出只有姓名和年级。其他数值变量只是 return 一些奇怪的符号。希望问题清楚,谢谢!
这段代码没有多大意义:
/* Format all data to be string */
sprintf(str, "%s ", (char*)(currNd -> data)); (1)
printf("%s\n", str); (2)
因为(1)表示currNd->data其实是一个字符串。 如果 currNd->data 是字符串,则不需要将其转换为临时(malloced)字符串。
这一行就足够了:
printf("%s\n", currNd -> data);
关于代码我们不能说太多,因为我们不知道 push
和 LinkedList
。但我假设 push
需要做两件不同的事情。如果参数是string
,它应该存储为string
。如果参数是 integer
它应该将其存储为 integer
.
因此,函数 push
似乎缺少枚举类型参数。
push( stack, name, TT_TYPE_STRING ); // will store the string
push( stack, id, TT_TYPE_INT ); // will store the integer
push( stack, age, TT_TYPE_INT ); // will store the integer
关于函数 toString()
它应该检查项目的类型。如果是字符串,前面的printf("%s", ...)
应该可以,如果是整数,应该调用printf(%d", ...)
.
所以我希望你的数据结构遗漏了一个重要的点:
struct ListItem
{
enum ItemType; /* INT OR String OR Something */
int IntValue;
char *StringValue;
};