Thread: Returning a pointer from a function just isn't working

  1. #1
    Registered User
    Join Date
    Aug 2022
    Posts
    40

    Returning a pointer from a function just isn't working

    Happy Labor Day Weekend. I've got a pointer question that I haven't been able to resolve. I'm trying to pass a string to a function which removes "mdata," from the beginning of the string and then returns a pointer to the new string. It ain't werkin... I'm not defining my pointer variables correctly or something.

    Code:
    #include<stdio.h>
    #include<string.h>
    
    
    char *rmDataForDBmarker(char *stringToClean, char *whatToRemove);
    
    
    int main()
    {
        char logToKeep[] = "mdata,";                                 // this is the key word of the log entry to keep
        char *logFinder = "mdata,b1a571,3324 d,123.46,987.99";
        char *dataMarkerRmvd;
    
    
        printf("\n string before the function: %s", logFinder);
        dataMarkerRmvd = rmDataForDBmarker(logFinder, logToKeep);
        printf("\n back from rmDataForDBmarker(): dataMarkerRmvd = %s", dataMarkerRmvd);
    }
    
    
    char *rmDataForDBmarker(char *stringToClean, char *whatToRemove){ // in this case
        printf("\n Now the code is in the function...");
        printf("\n The next stringToClean is = %s", stringToClean);
        int i, j = 0, k = 0,n = 0;
        int flag = 0;
        char *neww[100];                        // declaring neww as a pointer
    
    
        for(i = 0 ; stringToClean[i] != '\0' ; i++)
        {
            k = i;
            while(stringToClean[i] == whatToRemove[j])
            {
                i++,j++;
                if(j == strlen(whatToRemove))
                {
                    flag = 1;
                    break;
                }
            }
        j = 0;
        if(flag == 0){
            i = k;
        }
        else{
            flag = 0;
        }
        printf("\nstringToClean[i] = %c", stringToClean[i]);
        n++;
        printf("\ni=%d  n=%d", i, n);
        neww[n] = stringToClean[i];
        printf("\n neww[n] = %c", neww[n]);
        }
        neww[n] = '\0';
        printf("\n Here's the original string: %s", stringToClean);
        printf("\n String with %s removed: %s", whatToRemove, neww);
        printf("\n The memory location of neww: %p", neww);
        return neww;                        // trying to return the pointer to neww
    }
    When I compile it, the build log provides some insight that I don't understand:
    Code:
    C:\work\arduino\trash\Untitled6.c|46|warning: assignment to 'char *' from 'char' makes pointer from integer without a cast [-Wint-conversion]|
    
    C:\work\arduino\trash\Untitled6.c|53|warning: returning 'char **' from a function with incompatible return type 'char *' [-Wincompatible-pointer-types]|
    
    C:\work\arduino\trash\Untitled6.c|53|warning: function returns address of local variable [-Wreturn-local-addr]|
    I can see in the output that the string looks to be written to the "neww" string. The output looks like this:

    Returning a pointer from a function just isn't working-pointerissue-png

    I'm a novice coder. If someone could point me in the right direction, I'd appreciate it.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,430
    There are two main problems.
    1. char *neww[100];
    isn't an array of chars, nor even a simple char pointer.
    It's an array of pointers.

    2. You can't return a pointer to a local variable
    This is your 3rd error message.

    The usual means of getting a string result is to pass in the output string pointer as a parameter, like so.
    Code:
    #include<stdio.h>
    #include<string.h>
    
    void rmDataForDBmarker(char *stringToClean, char *whatToRemove, char *answer);
     
    int main()
    {
        char logToKeep[] = "mdata,";                                 // this is the key word of the log entry to keep
        char *logFinder = "mdata,b1a571,3324 d,123.46,987.99";
        char dataMarkerRmvd[100] = {};
    
        printf("string before the function: %s\n", logFinder);
        rmDataForDBmarker(logFinder, logToKeep, dataMarkerRmvd);
        printf("back from rmDataForDBmarker(): dataMarkerRmvd = %s\n", dataMarkerRmvd);
    }
     
    void rmDataForDBmarker(char *stringToClean, char *whatToRemove, char *answer){ // in this case
        printf("Now the code is in the function...\n");
        printf("The next stringToClean is = %s\n", stringToClean);
        char *p = strstr(stringToClean, whatToRemove);
        if ( p ) {
            p += strlen(whatToRemove);  // get to the end of the marker
            strcpy(answer,p);           // copy to the answer
        }
    }
    Code:
    $ gcc foo.c
    $ ./a.out 
    string before the function: mdata,b1a571,3324 d,123.46,987.99
    Now the code is in the function...
    The next stringToClean is = mdata,b1a571,3324 d,123.46,987.99
    back from rmDataForDBmarker(): dataMarkerRmvd = b1a571,3324 d,123.46,987.99
    It's usual for the newlines to be at the end of printf strings, not at the beginning.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Aug 2022
    Posts
    40
    Hey Salem, Thank you for responding. I've reworked my actual source (162 lines) to use the code you provided. I'm amazed at how you took the crazy loop that I had to remove the beginning characters with something much easier to comprehend.

    To incorporate your code into slightly large program, I added some code just after your line:
    Code:
    strcpy(answer,p);           // copy to the answer
    I added:
    Code:
            // insert write to disk
            printf("\n answer = %s", answer);
            FILE *fileDst = fopen("entriesForDBimport.txt", "w");
            if (fileDst == NULL){
                printf("\nUnable to open the file to write to.");
                exit(1);
            }
            else {
                fputs(answer, fileDst);
            }
    
    
            fclose(fileDst);
            // end added code
    The above works great for me in a test program but, not when I make it part of my larger program which to me seems almost exactly like the function you posted for me:
    Code:
    void rmDataForDBmarker(char *stringToClean, char *whatToRemove, char *answer){ // in this case
        printf("Now the code is in the function...\n");
        printf("The next stringToClean is = %s\n", stringToClean);
        char *p = strstr(stringToClean, whatToRemove);
        if ( p ) {
            p += strlen(whatToRemove);  // get to the end of the marker
            strcpy(answer,p);           // copy to the answer
            printf("\n answer = %s", answer);
    
    
            // insert write to disk
            FILE *fileDst = fopen("entriesForDBimport.txt", "w");
            if (fileDst == NULL){
                printf("\nUnable to open the file to write to.");
                exit(1);
            }
            else {
                fputs(answer, fileDst);
            }
            fclose(fileDst);
            // end added code
        }
    }
    Here is the log on my console:
    Returning a pointer from a function just isn't working-pointerissue2-jpg
    The Entry is not added to the log. Anything obviously wrong?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,430
    The writing to the file should be in the caller to rmDataForDBmarker, not in the function itself.

    The ideal function is small, and does exactly one thing described by the name of the function.

    FWIW, opening the file in "w" mode will lose any previous contents.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Aug 2022
    Posts
    40
    Thank you Salem for all your help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function returning pointer variable...
    By yj1214 in forum C Programming
    Replies: 3
    Last Post: 11-01-2015, 03:23 PM
  2. Need help with returning pointer from function
    By Hectickz in forum C Programming
    Replies: 8
    Last Post: 11-21-2014, 02:56 PM
  3. Function returning *this pointer
    By 4c0 in forum C++ Programming
    Replies: 3
    Last Post: 02-25-2013, 05:35 AM
  4. Address off when returning pointer from function.
    By dtow1 in forum C Programming
    Replies: 3
    Last Post: 09-14-2011, 02:11 PM
  5. function returning pointer
    By blue_gene in forum C Programming
    Replies: 7
    Last Post: 04-19-2004, 02:35 PM

Tags for this Thread