简体   繁体   中英

Problem with processing individual strings stored in an array of pointers to multiple strings in C

An array of pointers to strings is provided as the input. The task is to reverse each string stored in the input array of pointers. I've made a function called reverseString() which reverses the string passed to it. This functions works correctly as far as i know.

The strings stored/referenced in the input array of pointers are sent one by one to the reverseString() function. But the code hangs at some point in the reverseString() function when the values of the passed string are swapped using a temp variable. I can't figure out why the code is hanging while swapping values. Please help me with this.

The code is as follows:

#include <stdio.h>
void reverseString(char*);

int main()
{   char *s[] = {"abcde", "12345", "65gb"};
    int i=0;
    for(i=0; i< (sizeof(s)/sizeof(s[0]) ); i++ )
    {   reverseString(s[i]);
        printf("\n%s\n", s[i]);
    }

    getch();
    return 0;
}//end main

void reverseString(char *x)
{   int len = strlen(x)-1;
    int i=0; 
    char temp;
    while(i <= len-i)
    {   temp = x[i];
        x[i] = x[len-i];
        x[len-i] = temp;
            i++;
    }
}//end reverseString

You are trying to change string literals.

String literals are usually not modifiable, and really should be declared as const .

const char *s[] = {"abcde", "12345", "65gb"};
/* pointers to string literals */

If you want to make an array of modifiable strings, try this:

char s[][24] = {"abcde", "12345", "65gb"};
/* non-readonly array initialized from string literals */

The compiler will automatically determine you need 3 strings, but it can't determine how long each needs to be. I've made them 24 bytes long.

The strings ("abcde" etc) could be stored in readonly memory. Anything is possible when you try to modify those strings, therefore. The pointers to the strings are modifiable; it is just the strings themselves that are not.

You should include <string.h> to obtain the declaration of strlen(3) , and another header to obtain the function getch() - it is not in <stdio.h> on my MacOS X system (so I deleted the call; it is probably declared in either <stdio.h> or <conio.h> on Windows).

Hope this helps you! what i am doing here is that i am going to the address of the last character in the string then printing them all by decreasing the pointer by 1 unit (for character its 2 bytes(please check)).

//program to reverse the strings in an array of pointers
#include<stdio.h>
#include<string.h>
int main()
{
    char *str[] = {
        "to err is human....",
        "But to really mess things up...",
        "One needs to know C!!"
    };
    int i=0;    //for different strings
    char *p;    //declaring a pointer whose value i will be setting to the last character in 
                //the respective string
    while(i<3)  
    {
        p=str[i]+strlen(str[i])-1;
        while(*p!='\0')
        {
            printf("%c",*p);
            p--;
        }
        printf("\n");       
        i++;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM