0

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

Mooing Duck
  • 64,318
  • 19
  • 100
  • 158
user2303633
  • 39
  • 1
  • 8
  • 5
    Unless you want a C++ solution, I suggest not using the C++ tag. – juanchopanza Jun 14 '13 at 08:18
  • file is a pointer to char array you cannot change it to input...you can loop through input and set file – sethi Jun 14 '13 at 08:19
  • If you don't know how big the user input will be, you should really be looking into getting input with `fgets`. – Nobilis Jun 14 '13 at 08:20
  • @sethi: `file` is a char array, it is not a pointer. Don't confuse people – Mooing Duck Jan 20 '14 at 23:44
  • @MooingDuck in the context of (*myPointer).file = input; line it is a pointer to character array... – sethi Jan 21 '14 at 09:18
  • @sethi: In the context of that line, it's still an array. This is clearly demonstratable in C++. In C, it cannot be demonstrated, but the C specification also says it is an array. – Mooing Duck Jan 21 '14 at 16:53

9 Answers9

3

This is a C++ solution:

#include <string>
#include <iostream>

struct Node {
 std:string file;
};

then

Node node;
std::getline(std::cin, node.file);
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
2

You should do this :

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

You don't need to copy again your buffer.

nouney
  • 4,363
  • 19
  • 31
  • And how do you protect against buffer overflow? – trojanfoe Jun 14 '13 at 08:19
  • 1
    You can't with `scanf()`. Take a look [at this question](http://stackoverflow.com/questions/1621394/how-to-prevent-scanf-causing-a-buffer-overflow-in-c) – nouney Jun 14 '13 at 08:20
  • Oh, but that's just an example... In fact, I need a variable "input", because I'll use it in other functions... and then, only in a few cases I'll need to do (*myPointer).file = input.. In some cases I must not do it... – user2303633 Jun 14 '13 at 08:22
  • 1
    Then you will have to use `strncpy()` to copy your `input` variable into the `file` member of your struct. – nouney Jun 14 '13 at 08:24
1
(*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.

rectummelancolique
  • 2,247
  • 17
  • 13
  • `file` is an array, not a pointer. It can't be reassigned, so your first sentence is wrong. – Mooing Duck Jan 20 '14 at 23:45
  • I should clarify, the rest of this answer is 100% correct and helpful and awesome, and the first sentence is only _pedantically_ wrong. – Mooing Duck Jan 21 '14 at 16:54
0

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

Marius Bancila
  • 16,053
  • 9
  • 49
  • 91
0

can you try:

    gets(input);
    strcpy(mypointer->file, input);
Rhunita
  • 96
  • 2
0

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().

Omkant
  • 9,018
  • 8
  • 39
  • 59
  • Oh, I know I must allocate memory, but that's not the my actual code.. it's just an example with the important part.. Thank you! – user2303633 Jun 14 '13 at 08:28
0

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.

Charles0429
  • 1,406
  • 5
  • 15
  • 31
0

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.

xqterry
  • 632
  • 4
  • 10
0

It is more simple:

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

It is safe coping of string:

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