1

I have a problem with my code when I'm trying to store and print a string in a struct.

I have extracted the essential parts of the code here:

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

struct post                 
{
    char name[30];          
    int tel;            
    struct post *next;      
};

typedef struct post Post;

Post *head = NULL;      
Post *current;              

Post* CreateList(char tempname[30], int temptel);

Post* CreateList(char tempname[30], int temptel)
{   
    Post *ptr = (Post*)malloc(sizeof(Post));

    strcpy(ptr->name, tempname);    
    ptr->tel = temptel;
    ptr->next = NULL;

    printf("\n creating list with headnode as [%s]\n",tempname);

    head = current = ptr;
    return ptr;
}

int main()
{
    char tempname[30];
    int temptel;

    Post * ptr;

    printf("[Name] [Number] = ");
    scanf("%s %d", &tempname[30], &temptel);
    CreateList(tempname, temptel);
}

In the main you are supposed to insert a name and number and in CreateList it creates the first node of a linked list. When I'm trying to print the string with the name some garbage appears but the number is printed all fine.

I suspect that tempname is not really sent from main to CreateList even though i tried to send it as an argument in the function. I have tried to print the name stored in the struct as well:

    printf("\n creating list with headnode as [%s]\n",ptr->name);

but this also fails.

So the problem appears when I'm trying to print the name but since I've tried really many ways of printing it (with strcpy(), strncat(), strdup()) I start to suspect that i'm never actually storing the string in the ptr->name. So I'm either printing it wrong or storing it wrong.

I would really appreciate any help or hints towards a solution on this problem! Thanks in advance.

3 Answers 3

1

When you do:

scanf("%s %d", &tempname[30], &temptel);

You are passing, as address where to start writing, the memory location that follows the last element of your string (the last element of the string is tempname[29]). This means also that you are going to write outside your allocated memory, causing dangerous behaviour. Instead, you should pass the address of the first character of the string which is:

scanf("%s %d", &(tempname[0]), &temptel);

or more simply:

scanf("%s %d", tempname, &temptel);

I also suggest that you define your array size as a constant, and replace all the 30 with the constant name.

#define MY_STRING_SIZE 30
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much! That solved it, feels a bit stupid now when i actually see the mistake. Of course that is a great point to have the array size as a constant, the code is under development so it was more crucial to solve the error.
@user2551803 Glad it helped. My answer was faster, I think you should accept it.
1

You have a mistake:

scanf("%s %d", &tempname[30], &temptel);

should be:

scanf("%s %d", &tempname[0], &temptel);

Currently your input is stored after tempname last memory address.

1 Comment

Thank you so much! That solved it, feels a bit stupid now when i actually see the mistake.
0

As others have already pointed out, one big mistake was having scanf() write past the end of the array tempname, instead of writing in the array.

But I should add that it is also a good idea to limit the maximum number of characters that your reading should write to the array, by having your call to scanf() changed to something like

scanf("%29s %d", tempname, &temptel);

and then ensure that the string will be null-terminated, with the following statement.

tempname[29]='\0';

But there's even more: "%s" will not allow you to have spaces in the name.

You should also check the return value of scanf(), to see if both the string and the number have been successfully converted (a whitespace in the name, by the way, would prevent the conversion you the number, and might leave the input buffer in an inconsistent state).

scanf() is a powerful function, but it is definitely not trivial to use. You should read its documentation thoroughly.

2 Comments

Thanks you! You are right the "%s" wont allow spaces, instead the scanf() made the two nodes with the same number but the names were separated by the space. This could be a problem if I wanted to store the full name. But I think the easiest way of solving that would be to just store first name and last name in different variables.
@Paulo1205 But there's even more: "%s" will not allow you to have spaces in the name. Even if you escape them, for example with a backslash?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.