Aroli Marcellinus
Aroli Marcellinus

Reputation: 115

Passing char[] parameters doesn't work at all

Okay, so I have a code for trimming the duplicate characters in a string, based on here. Okay, I modified to become like this:

    void rem_dup(char str[]){
    int     char_check=0;
    int     i,j;
    char    ch;
    char    filterstr[256]; /* store the temporary strings */

    /* check from 1st character in the string */
    while(str[char_check]) {

            ch = str[char_check];

            i = j = char_check+1;

            filterstr[0] = str[0]; // added

    /* logic to remove the repeated character */
    while(str[i]) {
            if(str[i] != ch) {
                    filterstr[j] = str[i]; //modifid
                    j++;
            }
            i++;
    }

    filterstr[j]='\0'; //modified

    str = filterstr; //added

    char_check++;

    }

    printf("String after removing duplicates : %s\n",str); this
    }

And the I implement this by doing a simple script

    int main(){
        char Q[20];
        char E[26];

        fgets(Q,sizeof(Q),stdin);
        fgets(E,sizeof(E),stdin);

        rem_dup(Q);
        rem_dup(E);

        printf("\n%s\n%s\n",Q,E);
        return 0;
    }

I expect the output just like this

    0101010101
    ababababab
    String after removing duplicates : 01
    String after removing duplicates : ab
    01
    ab

However the output will become this

    0101010101
    ababababab
    String after removing duplicates : 01
    String after removing duplicates : ab
    0101010101
    ababababab

As you can see, the reality is the Q and E doesn't trimmed at all. If the array parameters is parameters-by-reference, and how can this kind of things happened to me?? Anyone has the answer to that?? I've searched many source and trying this only problem, but always failed. Thanks for answering or just giving opinion.

Upvotes: 2

Views: 271

Answers (4)

Dave
Dave

Reputation: 11162

you could simplify the whole thing to a few lines, avoid temporary storage like this:

void
rmdups(char *str)
{
     char *sp;
     for(sp=str; *sp; sp++){
          char *lo, *hi;
          for(lo=sp, hi=sp+1; *lo; hi++)
               if(*hi!=*sp)
                   *++lo = *hi;
     }
}

Upvotes: 0

David Heffernan
David Heffernan

Reputation: 612794

The line

str = filterstr;

is not doing what you thing it is. That code simply changes the local copy of the pointer to the buffer. It does not change the buffer's contents.

You need to call

strcpy(str, filestr);

just before the function returns, i.e. where your printf is now.

Looking at your logic, I believe it will fail for more complex inputs. I'm not going to attempt to debug it for you because I am not 100% sure of what you want the code to do.

Upvotes: 2

minus
minus

Reputation: 706

The problem is when you pass in the pointer you are not getting a reference to the actual object but a copy of the pointer to the object or this case a buffer. So when you say

str = filterstr

it doesn't change the original pointer or the one in the main but the local copy. That's why you see the trimmed string inside the function and not outside.

Also since filterstr is also local variable, just changing the pointers will not work you need to use strcpy.

Upvotes: 0

Dennis
Dennis

Reputation: 14477

The line str = filterstr; makes str (and therefore, Q) point to filterstr. When the function exists, filterstr gets destroyed and Q regains its original value.

Upvotes: 0

Related Questions