开发者

C read string from std error

I want to read string from standart input and output it in on console. I use this way:

char* cmdline;

do{
    scanf("%s\n", &cmdline);
    printf(开发者_如何学编程"%s\n", cmdline);
}while(cmdline != "quit");

But this doesn't work. I have this error Segmentation fault (core dumped)


char* cmdline

is a pointer. You are not allocating space for storing the string. You should do:

cmdline = malloc(size_of_string);

for allocating dynamic memory for storing the string. Otherwise use an array of char instead of a pointer:

char cmdline[size_of_string];


cmdline is just a pointer -- you need to actually allocate space for it using malloc or a fixed-size array.


char cmdline[80];

do {
    scanf("%79s\n", cmdline);
    printf("%s\n", cmdline);
} while(strcmp(cmdline, "quit"));

I spotted three errors with your code:

  1. Not allocating memory for the input buffer
  2. Not passing a buffer to scanf
  3. Not using strcmp to compare strings


You have defined char *cmdline but not allocated the cmdline pointer

do

cmdline = malloc (sizeof (char) * n);

first

Adjust the string length n as per your need.

EDIT1: In your version when you use cmdline without allocation it, then actually cmdline contains a value which can be anything, and using that for memory access is an attempt to access some memory area which you do not know where and is not permitted in OSes with memory protection (all the OSes nowadays). So when you store something in cmdline it will go in an invalid location, which is not permitted and the OS will issue segmentation fault for illegal memory access.

After you have allocated the memory from the OS (heap) with the malloc call the cmdline will contain a value which will have a memory location address which was issued by the OS for your code and is reserved for you, in which you have permission to write. So referring the location with the variable makes a proper memory reference and you can you it as normal. Also note if you try to go beyond the allocated memory block, ie access beyond the n th location (if you have allocated n bytes), then you can also get a segfault, as the memory locations beyond that limit is not registered/allocated for you. Although you might not get a segfault in this case, but writing in such location can be unpredective.

The sole cause for an attempt to elaborate this is that this is a very common malpractice to define a char * and without allocating it use it in codes, because the old Turbo C++ 3.1 does not complain, which is used by a LOT of people out there. Then they call me and tell that the GCC compiler is broken as the code does not run in it and runs perfect in TC++ 3.1.

EDIT2: Or simply use static array

char cmdline[MAX_SIZE];

where MAX_SIZE is set as per your need

EDIT3: OMG

You have done cmdline != "quit" this will never work. The solution is

while (strcmp (cmdline, "quit") != 0);

in your code. This will match the cmdline string character by character with the static string "quit"

Your solution will never work because when you do cmdline != "quit" then simply two addresses are being compared. First, the cmdline represents the address which you allocated with the malloc call, Second the address of the string "quit" which lies inside the data section of the executable, or simply in some area inside the memory where your program is loaded, which you have no idea. Comparing these two values will not compare the contents of these addresses, ie will not compare the strings inside it.

EDIT4: Also scanf ("%s", &cmdline); is incorrect as cmdline itself represents the address of the location where you want to store the string. The correct code is:

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

int main (void)
{
  char *cmdline;
  cmdline = malloc (sizeof (char) * 128);       /* or whatever size */
  do
    {
      scanf ("%s", cmdline);
      printf ("%s\n", cmdline);
    }
  while (strcmp (cmdline, "quit") != 0);
  return 0;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜