#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    22
    Rep Power
    0

    C string problem.


    Hello. I am actually a student and I just started creating my own little programms but I have a problem. I tried to create a program which was supposed to reverse the word test(to tset). I managed to compile it but when I try to print the result nothing comes up. its empty. Heres the code.

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    char *convert(char *a);
    int main()
    {
    char a[5]="Test";
    char *p;
    p=convert(a);
    printf("%s", p);
    free(p);
    system("pause");
    }

    char *convert(char *a)
    {
    char *array;
    int i;
    array=(char *)malloc(5*sizeof(char));
    for (i=0;i<5;i++)
    {

    array[i]=a[5-i-1];
    }
    array[5]='\0';
    return array;
    }
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    Use code tags to post code here. That preserves your code's formatting and keeps it readable.

    Code:
    char *convert(char *a)
    {
        char *array;
        int i;
        array=(char *)malloc(5*sizeof(char));
        for (i=0;i<5;i++);
        {
            array[i]=a[5-i-1];
        }
        array[5]='\0';
        return array;
    }
    Analyze array:
    array[0] = 't'
    array[1] = 's'
    array[2] = 'e'
    array[3] = 't'
    array[4] = '\0'
    array[5] is outside the bounds of the 5-element array and so "does not exist", but in reality by writing to this location you are clobbering (overwriting) whatever other data or function control value is stored there. The Thing and The Hulk can clobber, but you must not. Therefore, array[5]='\0'; is just plain wrong and must be corrected. But that's not the problem you wrote to us about.

    This statement,
    array[i]=a[5-i-1];
    is clumsy and stylistically would be better rendered as
    array[i]=a[4-i];
    Actually, that a index would in a general solution be [strlen(a)=i]; in this case strlen(a) would be 4. The for-loop should also iterate for strlen(a) -- personally, I would have assigned that function's return value to a variable for reuse.
    But that's not the problem that you wrote to us about.

    Look at that for-loop. I tried to bold the semicolon at the end, but you still might not see it. What you are telling the compiler is that there is block of code attached to this for-statement, so it loops 5 times doing notion except to iterate the value of i, then, since i == 5, it clobbers whatever is there with whatever comes right before the array a (ie, what's at a[-1]) and moves on. As a result, the array you return is filled with garbage, since you've never actually written to it.

    Getting rid of that semicolon should clear up the problem you wrote to us about, but you have some other problems to clean up as well.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    22
    Rep Power
    0
    Originally Posted by dwise1_aol
    Use code tags to post code here. That preserves your code's formatting and keeps it readable.

    Code:
    char *convert(char *a)
    {
        char *array;
        int i;
        array=(char *)malloc(5*sizeof(char));
        for (i=0;i<5;i++);
        {
            array[i]=a[5-i-1];
        }
        array[5]='\0';
        return array;
    }
    Analyze array:
    array[0] = 't'
    array[1] = 's'
    array[2] = 'e'
    array[3] = 't'
    array[4] = '\0'
    array[5] is outside the bounds of the 5-element array and so "does not exist", but in reality by writing to this location you are clobbering (overwriting) whatever other data or function control value is stored there. The Thing and The Hulk can clobber, but you must not. Therefore, array[5]='\0'; is just plain wrong and must be corrected. But that's not the problem you wrote to us about.

    This statement,
    array[i]=a[5-i-1];
    is clumsy and stylistically would be better rendered as
    array[i]=a[4-i];
    Actually, that a index would in a general solution be [strlen(a)=i]; in this case strlen(a) would be 4. The for-loop should also iterate for strlen(a) -- personally, I would have assigned that function's return value to a variable for reuse.
    But that's not the problem that you wrote to us about.

    Look at that for-loop. I tried to bold the semicolon at the end, but you still might not see it. What you are telling the compiler is that there is block of code attached to this for-statement, so it loops 5 times doing notion except to iterate the value of i, then, since i == 5, it clobbers whatever is there with whatever comes right before the array a (ie, what's at a[-1]) and moves on. As a result, the array you return is filled with garbage, since you've never actually written to it.

    Getting rid of that semicolon should clear up the problem you wrote to us about, but you have some other problems to clean up as well.
    Thank you for the quick answer I managed to fix the problem.

IMN logo majestic logo threadwatch logo seochat tools logo