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

6
  • 5
    Unless you want a C++ solution, I suggest not using the C++ tag. Commented Jun 14, 2013 at 8:18
  • file is a pointer to char array you cannot change it to input...you can loop through input and set file Commented Jun 14, 2013 at 8:19
  • If you don't know how big the user input will be, you should really be looking into getting input with fgets. Commented Jun 14, 2013 at 8:20
  • @sethi: file is a char array, it is not a pointer. Don't confuse people Commented Jan 20, 2014 at 23:44
  • @MooingDuck in the context of (*myPointer).file = input; line it is a pointer to character array... Commented Jan 21, 2014 at 9:18

9 Answers 9

3

This is a C++ solution:

#include <string>
#include <iostream>

struct Node {
 std:string file;
};

then

Node node;
std::getline(std::cin, node.file);
Sign up to request clarification or add additional context in comments.

2 Comments

Almost. Drop the underscore on _Node :-)
@Angew thanks. I am trained not to see leading underscores :-)
2

You should do this :

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

You don't need to copy again your buffer.

4 Comments

And how do you protect against buffer overflow?
You can't with scanf(). Take a look at this question
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...
Then you will have to use strncpy() to copy your input variable into the file member of your struct.
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.

2 Comments

file is an array, not a pointer. It can't be reassigned, so your first sentence is wrong.
I should clarify, the rest of this answer is 100% correct and helpful and awesome, and the first sentence is only pedantically wrong.
0

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

2 Comments

I can't read directly because I actually will use the input in some "if cases"... So, only in a few cases I'll do (*myPointer).file = input...
but that is wrong, you must strcpy your input, not assign pointers to the buffer.
0

can you try:

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

Comments

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

1 Comment

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!
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.

Comments

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.

Comments

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;

Comments

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.