Hi to everyone!
I have a problem with the following code. Can someone please point to me the error?

#include <stdio.h>

struct network{
        char *name;
    }network1;

int counter=0;

main()
{
    void add_network(); 
    add_network();
    printf("You typed: %s\n", network1.name);
    printf("counter is %d\n", counter);
}


void add_network()
{
    char network_name[150];
    puts("Give me the name: ");
    gets(network_name);
    network1.name=network_name; 
    printf("You typed: %s\n", network1.name);

    counter=1;
    printf("counter is %d\n", counter);
}

The problem is that the printf in the add_network function types correctly the input name, but the printf in main types jumbling characters. counter is typed correctly twice.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

you can't copy a string into another by using = operator in c, use strcpy function

strcpy (network1.name , network_name);

First of all, thanks for your answer.

What if i tell you that i tested this without success?

I use visual studio as a compiler and it pops a "visual studio just-in-time debugger" reporting: "an unhandled win32 exception occured in ... .exe [5820]", asking me if i want to debug.

Also, i get the warnings: 'gets' and 'strcpy' were declared deprecated.

>you can't copy a string into another by using = operator in c, use strcpy function
While this is true (somewhat), that's not the problem since the OP is using pointers. The problem is that the pointer is pointing to a local variable. When add_network returns, network_name is destroyed and leaves a dangling pointer to destroyed memory in network1.name. strcpy can still solve the problem, but only if arrays (or dynamic memory) are used instead of pointers:

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

struct network{
    char name[150];
} network1;

int counter=0;

int main(void)
{
    void add_network();

    add_network();
    printf("You typed: %s\n", network1.name);
    printf("counter is %d\n", counter);

    return 0;
}

void add_network()
{
    char network_name[150];

    puts("Give me the name: ");
    gets(network_name);
    strcpy(network1.name, network_name);
    printf("You typed: %s\n", network1.name);
    counter=1;
    printf("counter is %d\n", counter);
}

>i get the warnings: 'gets' and 'strcpy' were declared deprecated.
Ignore them. Those are stupid ass warnings from Microsoft to pimp their "safe" string library. Though you should favor fgets instead of gets, since gets really cannot be made safe.

Edited 6 Years Ago by Narue: n/a

I see. Yes, when the add_network() returns the pointer points to nothing useful. So, i have to capture the information in some way and that is why you told me to use arrays.

But, because the rest of my program, uses pointers in "network1.name", i found a way to overcome the issue.
After reading the name by the user, i measure the length of the string and allocate a (strlen+1) space for the "network1.name". In this way, it works:

...
gets(network_name);
network1.name=malloc( ( strlen(network_name) + 1 )*sizeof(char));
strcpy (network1.name , network_name);
...

Thank you very much for your answers!
(that problem drove me crazy today for couple of hours.. :) )

Edited 6 Years Ago by gouzou: n/a

This article has been dead for over six months. Start a new discussion instead.