user2303633
user2303633

Reputation: 39

dealing with string in struct

my program must have a struct with a string, like this

typedef struct _Node {

 char file[MAX];

//other stuff...

} Node;

Node *myPointer;

So, in other function, I need to read a string from the user and pass to my "file" variable in the struct, something like this:

char input[MAX];
scanf("%s", input);
(*myPointer).file = input;

The problem is that the size of the user's string is variable, it doesn't compile... What can I do ?

//sorry for my English

Upvotes: 0

Views: 2125

Answers (9)

AlGol
AlGol

Reputation: 1

It is more simple:

scanf("%s", myPointer->file);

It is safe coping of string:

strncpy(myPointer->file, input, MAX);
myPointer->file[MAX - 1] = 0;

Upvotes: 0

xqterry
xqterry

Reputation: 652

If you just want to scanf a string with %s, I suggest you use fgets from stdin, it can limit max length.

If you want to scan more complex format from user inputs, you can use fgets read limit length string, then sscanf to get what you need.

Directly using scanf may cause potential security issue, be careful when you perform any string buffer operations in C.

Upvotes: 0

juanchopanza
juanchopanza

Reputation: 227608

This is a C++ solution:

#include <string>
#include <iostream>

struct Node {
 std:string file;
};

then

Node node;
std::getline(std::cin, node.file);

Upvotes: 3

Charles0429
Charles0429

Reputation: 1420

That's because the variable file in your struct is a static array address(the same as char array[]) when the struct is defined and allocated memory.So it cannot be assigned a new string array address to it.Thus you can modify your code in the following ways:

1.As nouney answered, scanf("%s", (*myPointer).file);

2.Use strcpy function to copy the array input to myPointer.file array

3.Redefine your struct with Below:

typedef struct _Node {

 char *file;

//other stuff...

} Node;

Node *myPointer;

then allocate the memory of input use malloc function, for example:

input = (char *)malloc(sizeof(char) * MAX);
scanf("%s", input);
myPointer->file = input;

do not forget to free array file after you get done.

Upvotes: 0

rectummelancolique
rectummelancolique

Reputation: 2247

(*myPointer).file = input;

Only makes the member file point to the same address as input. If you want to copy the contents of input into myPointer->file then you have to use strcpy or strncpy. Also you need to make sure that your string is null terminated.

memset(myPointer->file, 0, MAX);
strncpy(myPointer->file, input, MAX-1);

The above is kind of generic, as in the input string can be any null terminated string. nouney's answer is the easiest if you need to take the string directly from user input.

Upvotes: 1

Omkant
Omkant

Reputation: 9234

You are declaring a type of struct using typedef and then you created pointer to that type but object is not created. So where do u want to put your data when you don't have memory.

Allocate memory for object first.

after taking input use strcpy().

Upvotes: 0

Rhunita
Rhunita

Reputation: 106

can you try:

    gets(input);
    strcpy(mypointer->file, input);

Upvotes: 0

Marius Bancila
Marius Bancila

Reputation: 16338

If this is C++, then use std::string. If this is C, then read the user input directly in (*myPointer).file.

Upvotes: 0

nouney
nouney

Reputation: 4411

You should do this :

scanf("%s", (*myPointer).file);

You don't need to copy again your buffer.

Upvotes: 2

Related Questions