[英]string.h and strncpy with pointers in C
I'm trying to create a user input created list that contains a structure with one int and two strings. 我正在尝试创建一个用户输入创建的列表,该列表包含一个具有一个int和两个字符串的结构。 But i seem unable to use correctly the strncopy from the string.h.
但是我似乎无法从string.h中正确使用strncopy。 I'm supposed to use the order of the parameters like: 1. name of pointer 2. string to be copied 3. string length
我应该使用以下参数的顺序:1.指针的名称2.要复制的字符串3.字符串长度
The error i get says that 'name' and 'lastn' which are strings are not declared...so what am i missing here? 我得到的错误是没有声明字符串的'name'和'lastn'...所以我在这里错过了什么?
CODE 码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct stats
{
int age;
char name[25];
char lastn[25];
struct stats *next;
};
void fill_structure(struct stats *s);
struct stats *create(void);
int main()
{
struct stats *first;
struct stats *current;
struct stats *new;
int x = 5;
//create first structure
first = create();
current = first;
for(x=0; x<5; x++)
{
if(x==0)
{
first = create();
current = first;
}
else
{
new = create();
current->next = new;
current = new;
}
fill_structure(current);
}
current->next = NULL;
current = first; //reset the list
while(current)
{
printf("Age %d, name %s and last name %s", current->age, strncpy(current->name, name, strlen(name)), strncpy(current->lastn, lastn, strlen(lastn)));
}
return(0);
}
//fill a structure
void fill_structure(struct stats *s)
{
printf("Insert Age: \n");
scanf("%d", &s->age);
printf("Insert Name: \n");
scanf("%s", &s->name);
printf("Insert Last Name: ");
scanf("%s", &s->lastn);
s->next = NULL;
}
//allocate storage for one new structure
struct stats *create(void)
{
struct stats *baby;
baby = (struct stats *)malloc(sizeof(struct stats));
if( baby == NULL)
{
puts("Memory error");
exit(1);
}
return(baby);
};
strncpy(current->name, name, strlen(name))
^ ^
You didn't declare any object named name
. 您没有声明任何名为
name
对象。 In your program the only name
identifier is the name
member of struct stats
structure type. 在您的程序中,唯一的
name
标识符是struct stats
结构类型的name
成员。
The following line uses name
and lastn
, which are not defined. 以下行使用
name
和lastn
,它们没有定义。
printf("Age %d, name %s and last name %s", current->age, strncpy(current->name, name, strlen(name)), strncpy(current->lastn, lastn, strlen(lastn)));
It's not clear what you are trying to accomplish by the calls to strncpy
here. 目前尚不清楚通过调用
strncpy
来完成的工作。 It will be sufficient to use: 足够使用:
printf("Age %d, name %s and last name %s", current->age, current->name, current->lastn);
Also, while(current)
will run for ever since you are not changing current
in the loop. 另外,
while(current)
将永远运行,因为您无需在循环中更改current
。 Use: 采用:
while(current)
{
printf("Age %d, name %s and last name %s", current->age, current->name, current->lastn);
current = current->next; // Need this
}
In fill_structure
, instead of: 在
fill_structure
,而不是:
scanf("%s", &s->name);
scanf("%s", &s->lastn);
use 采用
scanf("%s", s->name); // Drop the &
scanf("%s", s->lastn);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.