的malloc和scanf [英] Malloc and scanf
问题描述
我在一些脚本语言还算称职,但我终于强迫自己去学习生C.我只是用一些基本的东西(I / O现在)玩耍。我该如何分配堆内存,存储在内存中分配一个字符串,然后吐出来背出来了呢?这就是我现在所拥有的,我怎样才能使它正常工作?
的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;INT主(INT ARGC,CHAR *的argv [])
{
字符* toParseStr =(字符*)malloc的(10);
scanf函数(输入一个字符串,&安培; toParseStr);
的printf(%S,toParseStr);
返回0;
}
目前我越来越喜欢怪异的输出'8'\\'。
的char * toParseStr =(字符*)malloc的(10);
的printf(请在此处输入字符串:);
scanf函数(%S,toParseStr);
的printf(%S,toParseStr);
免费(toParseStr);
首先,在 scanf函数
的字符串指定它要接收输入。为了接收键盘输入之前显示的字符串,用的printf
,如图所示。
其次,你不需要解引用 toParseStr
,因为它是你与的malloc $分配的指向大小为10的字符数组C $ C>。 如果你使用的是功能,将它指向另一个内存位置,,然后
&放大器; toParseStr
要求
例如,假设你想写一个函数来分配内存。然后,你需要&放大器; toParseStr
,因为你改变指针变量的内容(这是内存中的地址 - 您可以通过打印看到自己的内容)。
无效AllocateString(字符** ptr_string,const int的N)
{
* ptr_string =(字符*)malloc的(N)
}
正如你所看到的,它接受的char ** ptr_string
其内容,它储存的指针的内存位置的指针会商店现在的 N
字节的分配块的第一个字节(内存地址(的malloc后
操作)它自从它的一些垃圾内存地址时不被初始化)。
INT主(INT ARGC,CHAR *的argv [])
{
字符* toParseStr;
const int的N = 10;
的printf(垃圾:%P \\ N,toParseStr);
AllocateString(安培; toParseStr,N);
的printf(%d字节一个连续数组的第一个元素的地址:%P \\ N,N,toParseStr); 的printf(请在此处输入字符串:);
scanf函数(%S,toParseStr);
的printf(%S,toParseStr);
免费(toParseStr); 返回0;
}
第三,建议您分配的可用内存。虽然这是你的整个程序,当程序退出该内存将被释放,它仍然是很好的做法。
I'm fairly competent in a few scripting languages, but I'm finally forcing myself to learn raw C. I'm just playing around with some basic stuff (I/O right now). How can I allocate heap memory, store a string in the allocated memory, and then spit it back out out? This is what I have right now, how can I make it work correctly?
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
char *toParseStr = (char*)malloc(10);
scanf("Enter a string",&toParseStr);
printf("%s",toParseStr);
return 0;
}
Currently I'm getting weird output like '8'\'.
char *toParseStr = (char*)malloc(10);
printf("Enter string here: ");
scanf("%s",toParseStr);
printf("%s",toParseStr);
free(toParseStr);
Firstly, the string in scanf
is specifies the input it's going to receive. In order to display a string before accepting keyboard input, use printf
as shown.
Secondly, you don't need to dereference toParseStr
since it's pointing to a character array of size 10 as you allocated with malloc
. If you were using a function which would point it to another memory location, then &toParseStr
is required.
For example, suppose you wanted to write a function to allocate memory. Then you'd need &toParseStr
since you're changing the contents of the pointer variable (which is an address in memory --- you can see for yourself by printing its contents).
void AllocateString(char ** ptr_string, const int n)
{
*ptr_string = (char*)malloc(n)
}
As you can see, it accepts char ** ptr_string
which reads as a pointer which stores the memory location of a pointer which will store the memory address (after the malloc
operation) of the first byte of an allocated block of n
bytes (right now it has some garbage memory address since it is uninitialized).
int main(int argc, char *argv[])
{
char *toParseStr;
const int n = 10;
printf("Garbage: %p\n",toParseStr);
AllocateString(&toParseStr,n);
printf("Address of the first element of a contiguous array of %d bytes: %p\n",n,toParseStr);
printf("Enter string here: ");
scanf("%s",toParseStr);
printf("%s",toParseStr);
free(toParseStr);
return 0;
}
Thirdly, it is recommended to free memory you allocate. Even though this is your whole program, and this memory will be deallocated when the program quits, it's still good practice.
这篇关于的malloc和scanf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!