简体   繁体   English

简单的 c scanf 没有读取和解析我的输入

[英]simple c scanf is not reading and parsing my input

I have created Date struct to store date format as date , month , year which is inputed by the user as a single line string seperated by space.我创建了Date struct来将日期格式存储为date , month , year ,由用户输入为由空格分隔的单行字符串。

I want to split the input and store it accordingly.我想拆分输入并相应地存储它。 I have used simple C sscanf() to split and store my input in struct Date .我使用简单的C sscanf()将我的输入拆分并存储在struct Date But when I try to print my data it's showing some garbage value, I don't know what is missing.但是当我尝试打印我的数据时,它显示了一些垃圾值,我不知道缺少什么。

Help me to clear this issue.帮我解决这个问题。

My code:我的代码:

Input: 24 Jan 1980
output: 24 ? -978635323 

here is my C code.这是我的 C 代码。

struct Date 
{
  int date;
  char* month;
  int year;
};

int main()
{
  struct Date date[1];
  char* string;
  scanf("%s",string);
  sscanf(string,"%d %s %d",&date[0].date,date[0].month,&date[0].year);
  printf("%d %s %d",date[0].date,date[0].month,date[0].year);
  return 0;
}

Thanks in advance.提前致谢。

char* string is an uninitialized pointer, it cannot hold a string, you need to allocate memory, initialize it as an array of chars or point it to a variable with memory already allocated or to an existing array of chars. char* string是一个未初始化的指针,它不能保存字符串,您需要分配内存,将其初始化为字符数组或将其指向已分配内存的变量或现有的字符数组。 Your structure member month suffers from the same problem.您的结构成员month遇到了同样的问题。

A -Wall flag in your compiler should give you a warning:编译器中的-Wall标志应该给你一个警告:

 'string is used uninitialized in this function [-Wuninitialized]'

Another problem is that %s only gets a string, when it finds a blank space, it stops reading, so it can only read 24 in your case.另一个问题是%s只获取一个字符串,当它找到一个空格时,它会停止读取,因此在您的情况下它只能读取24

Though "%[^\\n]s" can work, a better solution would be to use fgets , it's a safer function since you can limit the size of the input to the size of receiving container, unlike scanf .尽管"%[^\\n]s"可以工作,更好的解决方案是使用fgets ,它是一个更安全的功能,因为您可以将输入的大小限制为接收容器的大小,这与scanf不同。

#include <stdio.h>
#define SIZE 100

struct Date 
{
  int date;
  char month[SIZE];
  int year;
};

int main()
{
  struct Date date[1];
  char string[SIZE];
  fgets(string, sizeof(string), stdin); 
  sscanf(string,"%d %s %d",&date[0].date, date[0].month, &date[0].year);
}

it works for me.这个对我有用。 You can check it.你可以检查一下。

struct Date
{
    int dat;
    char month[20];
    int year;
};

int main()
{
    struct Date date[1];
    char str[20];
    scanf("%[^\n]%*c", str);
    sscanf(str,"%d %s %d",&date[0].dat,&date[0].month,&date[0].year);
    printf("%d %s %d",date[0].dat,date[0].month,date[0].year);
    return 0;
}

EXPLANATION解释

In your code, you have declared two char * pointers char *month and char *string , and trying to store string values in them.在您的代码中,您已经声明了两个char *指针char *monthchar *string ,并尝试在其中存储字符串值。 You have to understand that these two are mere pointers.你必须明白,这两个只是指针。 All they can store are memory addresses to char arrays.它们只能存储char数组的内存地址。 It is true that an array variable is interchangeable with a pointer in C. But an array declaration also involves a size attribute.确实,数组变量在 C 中可以与指针互换。但数组声明也涉及 size 属性。

When you declare a character array as:当您将字符数组声明为:

char str[10];

It actually reserves a block of size 10 in memory, implicitly creates char *str , and stores the base address of the newly created block in str .它实际上在内存中保留了一个大小为 10 的块,隐式创建了char *str ,并将新创建的块的基地址存储在str But when you just declare char *str , all it does is create a pointer.但是当你只是声明char *str ,它所做的就是创建一个指针。 This doesn't have any associated memory block for the array.这没有任何关联的数组内存块。

That's why you have to declare a character array of some size before storing strings in it with scanf() .这就是为什么在使用scanf()将字符串存储在其中之前必须声明某个大小的字符数组的原因。 Or you have to dynamically allocate memory with calloc or malloc after declaring the char * pointer.或者您必须在声明char *指针后使用callocmalloc动态分配内存。 Only then can you store a string input in it.只有这样,您才能在其中存储字符串输入。

I have modified your code to work properly.我已修改您的代码以使其正常工作。 It produces the desired output now.它现在产生所需的输出。 Hope this helps.希望这可以帮助。

WORKING CODE工作代码

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

struct Date 
{
  int date;
  char month[10];
  int year;
};

int main()
{
  struct Date date[1];

  char string[20];

  scanf("%[^\n]s",string);
  sscanf(string,"%d %s %d",&date[0].date,date[0].month,&date[0].year);
  printf("%d %s %d",date[0].date,date[0].month,date[0].year);

  return 0;
}

EDIT编辑

They themselves are integers actually (as are all pointers).它们本身实际上是整数(就像所有指针一样)。
As mentioned by anastaciu , pointers are not equivalent to integers per se.正如anastaciu所提到的,指针本身并不等同于整数。 You can follow the links given in his comments for more knowledge.您可以按照他的评论中给出的链接获取更多知识。 I had mentioned that statement to assert the difference between pointers and array declarations.我已经提到该声明来断言指针和数组声明之间的区别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM