C 返回后,在函数中赋值的指针为空
制作将客户记录添加到文本文件的函数。 我已经做了一个函数,可以修剪客户名称等的前导空格和尾随空格,称为trimspaces addrecord函数用于处理记录在文件中的存储。它得到3个参数(姓名/地址/电话)。在存储操作之前,函数将使用trimspaces函数删除空白,然后将3个字符串合并为一个字符串C 返回后,在函数中赋值的指针为空,c,string,pointers,malloc,C,String,Pointers,Malloc,制作将客户记录添加到文本文件的函数。 我已经做了一个函数,可以修剪客户名称等的前导空格和尾随空格,称为trimspaces addrecord函数用于处理记录在文件中的存储。它得到3个参数(姓名/地址/电话)。在存储操作之前,函数将使用trimspaces函数删除空白,然后将3个字符串合并为一个字符串 #include <dirent.h> #include <sys/types.h> #include <sys/stat.h> //mkdir #includ
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h> //mkdir
#include <stdio.h> //printf
#include <errno.h> //error number
#include <unistd.h> //access
#include <string.h> //strcat
#include <ctype.h> //isspace
#include <stdlib.h>//malloc
int checkFile();
int makeFile();
int addRecord(char* name, char* addr, char* phon);
int searchRec(int column, char* value);
char* getRec(int recNo);
int getRecNo();
char* trimspaces(char* string,char*ptr);
int addRecord(char* name, char* addr, char* phon){
printf("\n- starting records addReord function -\n");
int success = 0;
char* namt = trimspaces(name,namt);
char* addt = trimspaces(addr,addt);
char* phot = trimspaces(phon,phot);
//this prints "trimmed words: , , "
printf("\n trimmed words: %s, %s, %s",namt,addt,phot);
/*
char*combined1 = strcat(namt,"|");
char*combined2 = strcat(combined1,addt);
char*combined3 = strcat(combined2,"|");
char*combined4 = strcat(combined3,phot);
printf("\nwords combined: %s",combined4);
*/
printf("\n- leaving records addrecord function -\n");
return success;
}
char* trimspaces(char* string,char*ptr){
printf("\n- entered trimspaces function -");
char *str= string;
int slen = strlen(str); //string length
int ctfor = 0; //counter forward
int ctbak = 0; //counter back
while(isspace(*str)){ str++; ctfor++; }; //count to start of word
while(*str){str++;}; //go to end
do{ str--; ctbak++; }while(isspace(*str)); //count from end to end of word
int cbako = (slen - ctbak) + 1; //counter back reversed
int wlen = cbako - ctfor; //get word length
printf("\nstr_len:%d,counter_fore:%d,counter_bak:%d,cbakreversed:%d,wlen:%d",slen,ctfor,ctbak,cbako,wlen);
while(*str){ str--; }
str++;
while(isspace(*str)){
str++;
}
char newStr[wlen]; //char pointer gives segmentation fault
memcpy(newStr,str,wlen);
printf("\n--%s--",newStr);
ptr = malloc(sizeof(newStr)+1);
ptr = newStr;
printf("\nPTR is : %s",ptr);
return ptr;
printf("\n- leaving trimspaces function -");
}
int main(){
addRecord("kara","19,sams st","993328");
}
我在主功能的输出中遇到了两个问题。首先是-printf处的打印字符串(“\n修剪的单词:%s,%s,%s”,namt,addt,phot);
内容如下:删减单词:,
我尝试了很多方法,但是返回的变量总是空的。我想知道我使用malloc和指针是否正确
第二个问题是
--19,sams st@--
PTR is : 19,sams st@
@--93328s W
@TR is : 993328s W
我不知道@和Ws来自哪里。当我用不同的值测试trimspaces函数时,它会打印正确的结果
我将在这里注意到,我在终端上使用了export PS1='\u@\h:'来获得更短的提示
如何让变量打印值?无需将
ptr
作为参数传递给trimspaces
调用
这是可行的(即使字符串
仅为空白或空):
char*trimspaces(char*string){
尺寸长度=strlen(字符串);
char*s=string;//开始
char*e=string+len-1;//结束
而(s=s&&isspace(*e))e--;
尺寸=e-s+1;
char*ptr=(char*)malloc(大小+1);
int i;
对于(i=0;i
如果遇到指针越界的问题(我对此表示怀疑),请尝试使用索引而不是指针:
char *trimspaces(char *string){
int len = strlen(string);
int s = 0; //start
int e = len - 1; //end
while(s <= e && isspace(string[s])) s++;
while(e >= s && isspace(string[e])) e--;
int size = e - s + 1;
char *ptr = (char *) malloc(size + 1);
int i;
for(i = 0; i < size; i++)
ptr[i] = string[i + s];
ptr[i] = '\0';
return ptr;
}
char*trimspaces(char*string){
int len=strlen(字符串);
int s=0;//开始
int e=len-1;//结束
而(s=s&&isspace(string[e])e--;
int size=e-s+1;
char*ptr=(char*)malloc(大小+1);
int i;
对于(i=0;i
根据给出的反馈,我修改了代码以纠正一些错误。这段代码可能不太合适,但仍然存在问题(显然,trimspaces函数的空白输入字符串会产生错误-我暂时不讨论这个问题,继续讨论)
经过深入研究后,我发现需要将指针传递给指针(**),malloc才能处理传递给其他函数的变量。我仍然不能很好地理解这些原则,但它似乎有效。在指针空间和字符数组上也使用strcat,而不是使用direct=运算符
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h> //mkdir
#include <stdio.h> //printf
#include <errno.h> //error number
#include <unistd.h> //access
#include <string.h> //strcat
#include <ctype.h> //isspace
#include <stdlib.h>//malloc
int addRecord(char* name, char* addr, char* phon);
void trimspaces(char* string,char**ptr);
int addRecord(char* name, char* addr, char* phon){
printf("\n- starting records addReord function -\n");
int success = 0;
char* namt = "";
trimspaces(name,&namt);
char* addt = "";
trimspaces(addr,&addt);
char* phot = "";
trimspaces(phon,&phot);
//this prints "trimmed words: , , "
printf("\n TRIMMED words: %s, %s, %s",namt,addt,phot);
printf("\n- leaving records addrecord function -\n");
return success;
}
void trimspaces(char* string, char** ptr){
printf("\n- entered trimspaces function -");
//PROBLEMS WITH 0 SIZE OR BLANK INPUT
char *str= string;
int slen = strlen(str); //string length
int ctfor = 0; //counter forward
int ctbak = 0; //counter back
while(isspace((unsigned char)*str)){ str++; ctfor++; }; //count to start of word
while(*str){str++;}; //go to end
//unsigned char
do{ str--; ctbak++; }while(isspace((unsigned char)*str)); //count from end to end of word
int cbako = (slen - ctbak) + 1; //counter back reversed
int wlen = cbako - ctfor; //get word length
printf("\nstr_len:%d,counter_fore:%d,counter_bak:%d,cbakreversed:%d,wlen:%d",slen,ctfor,ctbak,cbako,wlen);
while(*str){ str--; }
str++;
while(isspace((unsigned char)*str)){
str++;
}
char newStr[wlen+1]; //char pointer gives segmentation fault
memcpy(newStr,str,wlen); //not null terminated
newStr[wlen] = '\0';
printf("\n--%s--",newStr);
//PASS POINTER TO POINTER for malloc to work / malloc inside another function
*ptr = malloc(sizeof(newStr)+1); //memory
strcat(*ptr,newStr);
printf("\nPTR is : %s",*ptr);
printf("\n- leaving trimspaces function -");
}
int main(){
addRecord(" ertsfs "," 120,Dans st "," 111 000 222");
}
#包括
#包括
#包括//mkdir
#包括//printf
#包含//错误号
#包括//访问
#包括//strcat
#包括//isspace
#包括//malloc
int addRecord(字符*名称,字符*地址,字符*电话);
空格(字符*字符串,字符**ptr);
int addRecord(char*name,char*addr,char*phon){
printf(“\n-开始记录addReord函数-\n”);
int成功=0;
char*namt=“”;
修剪空间(名称和名称);
char*addt=“”;
修剪空间(添加和添加);
char*phot=“”;
修剪空间(phon和phot);
//这将打印“修剪的单词:,”
printf(“\n修剪的单词:%s,%s,%s”,namt,addt,phot);
printf(“\n-离开记录addrecord函数-\n”);
回归成功;
}
空格(字符*字符串,字符**ptr){
printf(“\n-输入的trimspaces函数-”);
//0大小或空白输入的问题
char*str=string;
int slen=strlen(str);//字符串长度
int ctfor=0;//计数器向前
int-ctbak=0;//计数器返回
while(isspace((unsigned char)*str)){str++;ctfor++;};//计数到单词的开头
while(*str){str++;};//转到末尾
//无符号字符
do{str--;ctbak++;}while(isspace((unsigned char)*str));//从单词的结尾到结尾进行计数
int cbako=(slen-ctbak)+1;//反向计数器
int-wlen=cbako-ctfor;//获取字长
printf(“\nstr\u len:%d,counter\u fore:%d,counter\u bak:%d,cbakreversed:%d,wlen:%d”,slen,ctfor,ctbak,cbako,wlen);
while(*str){str--;}
str++;
while(isspace((无符号字符)*str)){
str++;
}
char newStr[wlen+1];//char指针给出分段错误
memcpy(newStr、str、wlen);//不以null结尾
newStr[wlen]='\0';
printf(“\n--%s--”,newStr);
//将指针传递给另一个函数中的malloc TO work/malloc的指针
*ptr=malloc(sizeof(newStr)+1);//内存
strcat(*ptr,newStr);
printf(“\nPTR为:%s”,*ptr);
printf(“\n-函数-”);
}
int main(){
addRecord(“ertsfs”,“120,Dans街”,“111 000 222”);
}
在对trimspaces
的调用中,参数namt
、addt
和phot
未初始化。此代码具有未定义的行为。在ptr=malloc(…);ptr=…
从malloc
返回的值丢失。这是内存泄漏。您创建了一个局部变量newStr[wlen]代码>。此变量(及其内容)在函数返回后消失。并且您复制的字符串不是以NULL结尾。trimspaces
返回newStr
(实际上是指向newStr
的第一个元素的指针),它是一个本地数组,在函数返回后不再存在。这是未定义的行为。在许多平台上,char
是有符号类型(即,它可以有负值)。将负值传递给isspace
具有未定义的行为(除非它是EOF
)。这应该是isspace((unsigned char)*str)
。在空字符串上失败。不要强制转换malloc()
。在空字符串上执行e=string+len-1
,即e=string-1
。这有未定义的行为。ptr
参数未使用。这是错误的。形成无效指针本身是无效的。
char *trimspaces(char *string){
int len = strlen(string);
int s = 0; //start
int e = len - 1; //end
while(s <= e && isspace(string[s])) s++;
while(e >= s && isspace(string[e])) e--;
int size = e - s + 1;
char *ptr = (char *) malloc(size + 1);
int i;
for(i = 0; i < size; i++)
ptr[i] = string[i + s];
ptr[i] = '\0';
return ptr;
}
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h> //mkdir
#include <stdio.h> //printf
#include <errno.h> //error number
#include <unistd.h> //access
#include <string.h> //strcat
#include <ctype.h> //isspace
#include <stdlib.h>//malloc
int addRecord(char* name, char* addr, char* phon);
void trimspaces(char* string,char**ptr);
int addRecord(char* name, char* addr, char* phon){
printf("\n- starting records addReord function -\n");
int success = 0;
char* namt = "";
trimspaces(name,&namt);
char* addt = "";
trimspaces(addr,&addt);
char* phot = "";
trimspaces(phon,&phot);
//this prints "trimmed words: , , "
printf("\n TRIMMED words: %s, %s, %s",namt,addt,phot);
printf("\n- leaving records addrecord function -\n");
return success;
}
void trimspaces(char* string, char** ptr){
printf("\n- entered trimspaces function -");
//PROBLEMS WITH 0 SIZE OR BLANK INPUT
char *str= string;
int slen = strlen(str); //string length
int ctfor = 0; //counter forward
int ctbak = 0; //counter back
while(isspace((unsigned char)*str)){ str++; ctfor++; }; //count to start of word
while(*str){str++;}; //go to end
//unsigned char
do{ str--; ctbak++; }while(isspace((unsigned char)*str)); //count from end to end of word
int cbako = (slen - ctbak) + 1; //counter back reversed
int wlen = cbako - ctfor; //get word length
printf("\nstr_len:%d,counter_fore:%d,counter_bak:%d,cbakreversed:%d,wlen:%d",slen,ctfor,ctbak,cbako,wlen);
while(*str){ str--; }
str++;
while(isspace((unsigned char)*str)){
str++;
}
char newStr[wlen+1]; //char pointer gives segmentation fault
memcpy(newStr,str,wlen); //not null terminated
newStr[wlen] = '\0';
printf("\n--%s--",newStr);
//PASS POINTER TO POINTER for malloc to work / malloc inside another function
*ptr = malloc(sizeof(newStr)+1); //memory
strcat(*ptr,newStr);
printf("\nPTR is : %s",*ptr);
printf("\n- leaving trimspaces function -");
}
int main(){
addRecord(" ertsfs "," 120,Dans st "," 111 000 222");
}