【问题标题】:string.h and strncpy with pointers in Cstring.h 和 strncpy 与 C 中的指针
【发布时间】:2015-09-29 17:44:53
【问题描述】:

我正在尝试创建一个包含一个 int 和两个字符串的结构的用户输入创建列表。但我似乎无法正确使用 string.h 中的 strncopy。 我应该使用参数的顺序,例如: 1. 指针名称 2.要复制的字符串 3.字符串长度

我得到的错误是没有声明作为字符串的'name'和'lastn'......所以我在这里缺少什么?

代码

#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);
};

【问题讨论】:

  • C 中的 cmets 是 /* */ 而不是 //。您的代码中的很多东西都是错误的和损坏的。
  • @Michi,任何现代版本的 C 都允许使用这两种 cmets。
  • 请始终缩进代码(切勿使用制表符进行缩进,因为每个文字处理器/编辑器的制表位/制表符宽度设置不同)建议每个缩进级别为 4 个空格,因为这不会占用可用宽度页面,即使使用可变宽度字体也可见。一致缩进意味着在每个左大括号 '{' 之后缩进,在每个右大括号 '}' 之前不缩进 为了可读性,包装代码块也是一个好习惯(if.else.while、for、do...while ) 有一个空行
  • 在 C 中,malloc() 和函数族的返回值是 void *。 void * 可以分配给任何其他指针。强制转换返回值只会使代码混乱,并且在执行维护时会让人头疼。
  • 在调用 scanf() 和函数族时,始终检查返回值(而不是参数值)以确保操作成功。使用 '%s' 格式说明符的第二次 scanf() 调用将失败。这是因为输入流中仍然存在“空白”,并且“%s”格式说明符不会跨“空白”输入字符%s' 格式说明符,用于将格式字符串修改为“%s”。注意前导空格,它将跳过“空白”

标签: c list pointers strncpy


【解决方案1】:
strncpy(current->name, name, strlen(name))
                         ^           ^

您没有声明任何名为 name 的对象。在您的程序中,唯一的 name 标识符是 name 结构类型的 name 成员。

【讨论】:

  • 那里还有很多其他问题,比如 lastn 也没有声明,比如 scanf("%s", &s->name); 当然没有检查scanf 可能的错误
【解决方案2】:

以下行使用了namelastn,它们没有被定义。

printf("Age %d, name %s and last name %s", current->age, strncpy(current->name, name, strlen(name)), strncpy(current->lastn, lastn, strlen(lastn)));

不清楚您在此处调用strncpy 的目的是什么。使用就足够了:

printf("Age %d, name %s and last name %s", current->age, current->name, current->lastn);

此外,while(current) 将永远运行,因为您没有在循环中更改 current。使用:

while(current)
{
   printf("Age %d, name %s and last name %s", current->age, current->name, current->lastn);
   current = current->next; // Need this
}

fill_structure,而不是:

scanf("%s", &s->name);
scanf("%s", &s->lastn);

使用

scanf("%s", s->name);   // Drop the &
scanf("%s", s->lastn);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-26
    • 1970-01-01
    • 1970-01-01
    • 2021-05-06
    • 1970-01-01
    相关资源
    最近更新 更多