#include<stdio.h>
int main ()
{
int *ip;
printf("Enter a no \n");
scanf("%d",ip);
printf("ip=%d",*ip);
}
Run Code Online (Sandbox Code Playgroud)
以上是我的程序,当我运行它时输出
./a.out
Enter a no
10
Segmentation fault
Run Code Online (Sandbox Code Playgroud)
我在gdb第6行检查了问题是什么
scanf("%d",ip);
所以我推断这ip是一个指向整数的指针并%d期望一个地址值.虽然我试图将一个整数传递给指定的值,ip所以我认为这是错误的.但在上述情况下,正确的做法是什么.我想使用扫描并为指向的地址分配一个整数值ip(在我的情况下,这是丢失的).
我引用了reallocmalloc(3)中定义的一部分(http://man.he.net/?topic=malloc§ion=all)
除非ptr为NULL,否则必须由之前调用malloc(),calloc()或realloc()返回.
如果我们打电话realloc给ptr那不是NULL,但是,之前的电话没有回复malloc(), calloc() 或者 realloc()?
realloc的行为是不确定的?还有另一个答案吗?
我有个问题.我正在为a分配空间char*,但是当我尝试free这个空间时,我的程序崩溃了.
这是代码
fullPath = (char *) malloc(strlen(path) + strlen(fileData->d_name) + 1);
if (fullPath == NULL)
{
handleErrors(ERR_ALOCATION_CODE);
}
sprintf(fullPath, "%s/%s", path, fileData->d_name);
//... some more code, I only use fullPath, I don't change it here
free(fullPath);
Run Code Online (Sandbox Code Playgroud)
上面的代码在尝试时失败了free.我将不胜感激.
我正在寻找一种方法,只将大写字母转换为用户输入字符串的小写字母.问题是我的条件被忽略了,每个字符都被改变了,而不仅仅是大写字母.为了记录,我还尝试使用atoi将字符转换为int,但遇到了与上面相同的问题.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char letter[100];
scanf("%s", letter);
int i;
for(i=0; letter[i]!='\0'; i++){
if((letter[i]>='A')||(letter[i]<='Z')){
letter[i]=letter[i]+32;
}
}
printf("%s", letter);
return 0;
}
Run Code Online (Sandbox Code Playgroud)