-1

I created a linked list that contains a char array. Then I tried to print it using %s and it would not print. I know I have to transform the char by adding in '\0' so that it can print using '/0'. But I'm not sure why.

For example if I input:

Bob 20

It should print:

New student created: Bob is 20 years old.

BUT: it's printing (without "Bob"):

New student created: is 20 years old.

#include <stdio.h>
#include <stdlib.h>

struct student {
      char name[50];
      int age;
      struct student *next;
};


struct student *createStudent(char studentName[], int studentAge);

int main(void) {
    struct student *studptr;
    int myAge;
    char myName[50];
    scanf("%s %d", myName, &myAge);
    studptr = createStudent(myName, myAge);
    printf("New student created: %s is %d years old.\n", studptr->name, studptr->age);
    free(studptr);
    return 0;
}

struct student *createStudent(char studentName[], int studentAge){

    struct student * ptr;
    ptr = (struct student *)malloc(sizeof(struct student));

    ptr->name[50] = studentName[50];
    ptr->age = studentAge;
    ptr->next = NULL;

    return ptr;
}

NOTE: I understand the code below will work and print the correct name, where I add an additional method to change the char array called copyStr(), but I'm not sure why....

#include <stdio.h>
#include <stdlib.h>

struct student {
      char name[50];
      int age;
      struct student *next;
};


struct student *createStudent(char studentName[], int studentAge);
void copyStr(char *source, char *target);

int main(void) {
    struct student *studptr;
    int myAge;
    char myName[50];
    scanf("%s %d", myName, &myAge);
    studptr = createStudent(myName, myAge);
    printf("New student created: %s is %d years old.\n", studptr->name, studptr->age);
    free(studptr);
    return 0;
}

struct student *createStudent(char studentName[], int studentAge){

    struct student * ptr;
    ptr = (struct student *)malloc(sizeof(struct student));

    //we need to translate the char into a string
    copyStr(studentName, ptr->name);


    ptr->name[50] = studentName[50];
    ptr->age = studentAge;
    ptr->next = NULL;

    return ptr;
}

void copyStr(char *source, char *target){

    int i = 0;
    while(source[i] != '\0')
    {
        target[i] = source[i];
        i++;
    }
    target[i] = '\0';
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Katie Melosto
  • 1,047
  • 2
  • 14
  • 35
  • 2
    `ptr->name[50] = studentName[50];` that's not how you copy arrays in C; that copies a single character. And worse, that invokes *undefined behavior* by access the arrays outside of their declared size. (both are `char[50]` and therefore only indexable from 0..49 inclusively). – WhozCraig Jan 01 '20 at 17:33
  • 1
    You are copying character and that too beyond the limits `ptr->name[50] = studentName[50]` **+** Use `strcpy` instead of some copy function – infinite Jan 01 '20 at 17:34
  • 1
    See [How to copy a char array in C?](https://stackoverflow.com/questions/16645583/how-to-copy-a-char-array-in-c) for an explanation of why copying strings requires a loop. – John Kugelman Jan 01 '20 at 17:37
  • 1
    The `copyStr` function is unnecessary. The standard library already has `strcpy`. – Lxer Lx Jan 01 '20 at 17:42

2 Answers2

2

Arrays do not have the assignment operator.

This statement

ptr->name[50] = studentName[50];

tries to assign the non-existent element with the index 50 of the array pointed to by the pointer studentName to the non-existent element of the array ptr->name.

Instead you should use the standard C function strcpy declared in the header <string.h>.

For example

strcpy( ptr->name, studentName );
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

Adding to @Vlad's answer, Right way to copy arrays is by iterating over them, thereby, picking each element in char array and copying it to new location. strncpy does exactly that along with buffer overflow checks, which makes it better than strcpy.

Please have a look at strcpy documentation, which says : "ensure size of the array pointed by destination shall be long enough to contain the same C string as source (including the terminating null character), and should not overlap in memory with source"

On a completely different side note, if you're looking for one-liner assignment, you're allowed to do this:

   char sample[] = {"randomText"};
   char sample2[11] = {"randomText"};
H S
  • 1,211
  • 6
  • 11