-1

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);
};
Mimic01
  • 107
  • 1
  • 12
  • comments in C are **/* */** not **//**. A lot of things in your code are wrong and broke. – Michi Sep 29 '15 at 17:53
  • 1
    @Michi, any modern version of C allows both kinds of comments. – user3629249 Sep 29 '15 at 17:56
  • Please consistently indent the code (never use tabs for indenting as each wordprocessor/editor has the tab stops/tab widths set differently) Suggest each indent level be 4 spaces as that does not eat up the available width of the page and is visible even with variable width fonts. Consistent indenting means to indent after every opening brace '{' and un-indent before every closing brace '}' For readability, it is also a good practice to wrap code blocks (if.else. while, for, do...while) with a blank line – user3629249 Sep 29 '15 at 17:59
  • in C, the returned value from malloc() and family of functions is a `void *`. A void * can be assigned to any other pointer. Casting the returned value just clutters the code and creates headaches when maintenance is being performed. – user3629249 Sep 29 '15 at 18:02
  • When calling scanf() and family of functions, always check the returned value (not the parameter value) to assure the operation was successful. The second call to scanf(), that uses the '%s' format specifier will fail. This is because there will be 'white space' still sitting in the input stream and the '%s' format specifier will not input characters across 'white space' Suggest, changing the format string for the calls to scanf() that use the '%s' format specifier to modify the format string to " %s". Notice the leading space, which will skip over 'white space' – user3629249 Sep 29 '15 at 18:07
  • 'new' is a reserved word in C++ and if you use a compiler that will compile C++, will cause problems. Strongly suggest replacing 'new' with a better, less problematic variable name. – user3629249 Sep 29 '15 at 18:09
  • When compiling, always enable all the warnings (for gcc, at a minimum use: `-Wall -Wextra -pedantic` ) Then the compiler would tell you about the many syntax errors in the code. the usage of non existent variables, the taking the address of an address, etc. BTW: in C, an array degrades to the address of the first byte of the array when the array name is used. There is a spurious ';' after the closing brace of the `create()` function – user3629249 Sep 29 '15 at 18:16
  • Please correct the many compile problems, consistently indent the code, add the checking for errors, an post an 'edit' with the corrected code. – user3629249 Sep 29 '15 at 18:18

2 Answers2

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

ouah
  • 142,963
  • 15
  • 272
  • 331
  • There a lot of another problems in there like lastn also undeclared, like **scanf("%s", &s->name);** and of course there is no check for scanf possible errors – Michi Sep 29 '15 at 17:52
1

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);
R Sahu
  • 204,454
  • 14
  • 159
  • 270