簡體   English   中英

返回后在函數中分配值的指針為空

[英]pointer that is assigned value in function is blank after returning

提供將客戶記錄添加到文本文件的功能。 我已經制作了一個功能,該功能可以裁剪掉客戶名稱等的前導空格和最后空格,稱為trimspaces。

addrecord函數用於處理文件中的記錄存儲。 它獲得3個參數(名稱/地址/電話)。 在存儲操作函數之前,將使用trimspaces函數刪除空格,然后將3個字符串組合為1個。

#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--之間的文本是帶前導/結尾空格的字符串,並加上修飾的單詞行說-修飾詞: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單詞:,,Ive嘗試了很多操作,但是返回的變量始終為空。 我想知道Im是否正確使用malloc和指針。

第二個問題是

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

我不知道@和Ws的來源。 當我用不同的值測試trimspaces函數時,它會打印正確的結果。

在這里,我將在終端上使用export PS1 ='\\ u @ \\ h:'來縮短提示時間。

我該怎么做才能使變量顯示值?

根據給出的反饋,Ive修改了代碼以糾正某些錯誤。 此代碼可能不太正確,仍然存在問題(顯然,trimspaces函數的空白輸入字符串會產生錯誤-我暫時不考慮此問題,暫時繼續討論)

經過一些挖掘后,我發現您需要將指針傳遞給指針(**),以便malloc在傳遞給其他函數的變量上工作。 仍然不能很好地理解這些原理,但是似乎可行。 還可以在指針空間和char數組上使用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");

}

無需將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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM