返回后,在函数中赋值的指针为空白

时间:2017-01-14 01:02:42

标签: c string pointers malloc

创建用于将客户记录添加到文本文件的功能。 我已经创建了一个功能,可以修剪客户名称等的前导和最后空格,称为trimspaces。

函数addrecord用于处理文件中的记录存储。它获得3个参数(名称/地址/电话)。在存储操作函数之前,将使用trimspaces函数删除空格,然后将3个字符串合并为一个。

#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");

}

这是输出: (我希望--text--之间的文本是带有前导/结束空格的字符串,以及用于说明的词语行 - TRIMMED单词:kara,19,sams st,993328)

- starting records addReord function -

- entered trimspaces function -
str_len:4,counter_fore:0,counter_bak:1,cbakreversed:4,wlen:4
--kara--
PTR is : kara
- entered trimspaces function -
str_len:10,counter_fore:0,counter_bak:1,cbakreversed:10,wlen:10
--19,sams st@--
PTR is : 19,sams st@
- entered trimspaces function -
str_len:6,counter_fore:0,counter_bak:1,cbakreversed:6,wlen:6
@--93328s W
@TR is : 993328s W
 TRIMMED words: , , 
- leaving records addrecord function -

我在主要功能的输出中遇到了2个问题。首先打印字符串 - printf(“\ n TRIMMED words:%s,%s,%s”,namt,addt,phot); 读:TRIMMED字:,,, 我尝试了很多东西,但返回的变量总是空白的。我想知道我是否正确使用malloc和指针。

第二个问题是

--19,sams st@--
PTR is : 19,sams st@
@--93328s W
@TR is : 993328s W

我不知道@和Ws来自哪里。当我使用不同的值测试trimspaces函数时,它会打印出正确的结果。

我会在这里注意到我在终端上使用导出PS1 ='\ u @ \ h:'来获得更短的提示。

我该怎么做才能让变量打印出来?

2 个答案:

答案 0 :(得分:0)

根据给出的反馈,我修改了代码以纠正一些错误。这段代码可能不太合适并且仍然存在问题(显然空白的输入字符串到trimspaces函数会产生错误 - 我只留下这个问题并暂时继续)

经过一些挖掘后,我发现你需要传递一个指向指针(**)的指针,以便malloc处理传递给不同函数的变量。仍然不明白这些原则,但它似乎工作。也可以在指针空间和char数组上使用strcat而不是使用direct =运算符。

Scanner

答案 1 :(得分:-1)

无需将ptr作为参数传递给trimspaces来电。

这是有效的(即使string只是空格或空白):

char *trimspaces(char *string){
    size_t len = strlen(string);
    char *s = string; //start
    char *e = string + len - 1; //end

    while(s <= e && isspace(*s)) s++;
    while(e >= s && isspace(*e)) e--;

    size_t size = e - s + 1;
    char *ptr = (char*) malloc(size + 1);

    int i;
    for(i = 0; i < size; i++)
        ptr[i] = s[i];
    ptr[i] = '\0';

    return 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;
}