简体   繁体   中英

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. But i seem unable to use correctly the strncopy from the string.h. I'm supposed to use the order of the parameters like: 1. name of pointer 2. string to be copied 3. string length

The error i get says that 'name' and 'lastn' which are strings are not declared...so what am i missing here?

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 . In your program the only name identifier is the name member of struct stats structure type.

The following line uses name and lastn , which are not defined.

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. 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. 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:

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

use

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM