这是我的代码:当我执行这个代码,我得到当我按下输入分割故障Ç段错误是由于错误的指针
#include <stdio.h>
#include <stdlib.h>
void getinfo(unsigned int a, unsigned int b, char **s);
int main(){
unsigned int len_max = 8;
unsigned int current_size = 0;
char *pStr = malloc(len_max);
if(pStr == NULL){
perror("\nMemory allocation\n");
return EXIT_FAILURE;
}
current_size = len_max;
printf("Inserisci hostname: ");
getinfo(len_max, current_size, &pStr);
printf("\nLa stringa inserita è: %s\n", pStr);
free(pStr);
return EXIT_SUCCESS;
}
void getinfo(unsigned int a, unsigned int b, char **pStr){
unsigned int i = 0;
char c = EOF;
while((c = getchar()) != '\n'){
*pStr[i++] = (char)c;
if(i == b){
b = i+a;
if((*pStr = realloc(*pStr, b)) == NULL){
perror("\nMemory allocation error\n");
exit(EXIT_FAILURE);
}
}
}
*pStr[i]='\0';
}
(后我已经写的字符串)。
我敢肯定,问题是功能(可能是问题是*的指针),但我不知道如何纠正它...
注意:'getchar'返回'int'而不是'char'。你应该考虑把'c'改成'int' –
哦谢谢:)我纠正了错误! – polslinux