0
#include <stdio.h>
#include <string.h>
void reverse(char * str[]) {
    int i;
    int reverse = sizeof(str);

    for(i=0;i<=sizeof(str);i++){
        *str[i]=*str[reverse];
        reverse--;
    }
}

main() {
        char *word;

        printf("Enter a word please=>");
        scanf("%s",word);

        reverse(word);      
        printf("%s",word);
}

I am trying to get a string input and pass it to reverse() function to reverse the word entered i.e ("abcd" -> "dcba"), however i am having some difficulty using pointers.

I am not able to change the values held by the char *word in the memory.

KAKAK
  • 879
  • 7
  • 16
  • 32
  • Welcome to the fiendishly difficult world of pointers and memory management. It's Really Hard. – Kerrek SB Jul 13 '13 at 17:05
  • `*str[reverse];` ---> seems you are going out of bounds – pinkpanther Jul 13 '13 at 17:06
  • 1
    1) `void reverse(char * str[]) {` str is not a string but an array of pointers (or a pointer to pointer) 2) sizeof does not do what you intend 3) `for(i=0;i<=sizeof(str);i++){` you probably want `<` herte (even though the `sizeof(str)` is clearly wrong – wildplasser Jul 13 '13 at 17:07
  • @pinkpanther i am just swapping place of the first digit with the last digit..its not using the function `reverse()`. maybe you got confused – KAKAK Jul 13 '13 at 17:08
  • @wildplasser the sizeof(str) is working fine at least in my code as it is giving me the correct array size. thanks for the help – KAKAK Jul 13 '13 at 17:09
  • `sizeof(str)` returns the size of a pointer - you want `strlen(str)` instead – Drew McGowen Jul 13 '13 at 17:11
  • It *could* give you the correct size iff that size happens to be equal to `sizeof (char**)` – wildplasser Jul 13 '13 at 17:11
  • @DeepakTivari what I'm saying is since indices in C are zero based you might have to use reverse-1.... – pinkpanther Jul 13 '13 at 17:18

1 Answers1

3

You haven't allocated any storage for word. Change

char *word;
scanf("%s",word);

to

char word[20];
scanf("%19s",word);

There are also a number of issues with reverse

  • Its signature accepts an array of strings rather than a single string (an array of chars).
  • You need to use strlen to calculate the length of a string. sizeof tells you the size of a char** on your platform.
  • You reverse the string twice so will currently reverse it then reinstate the original order.
  • Your reverse algorithm would include reversing the nul terminator. This wouldn't normally be considered part of a string and needs to stay at the end of the array to mark the end of the string.

The following (untested) function should be closer to what you want

void reverse(char* str) {
    int i;
    int len = strlen(str);
    for (int i=0; i<len/2; i++) {
        char tmp = str[i];
        str[i] = str[len-i-1];
        str[len-i-1] = tmp;
    }
}
simonc
  • 41,632
  • 12
  • 85
  • 103
  • 1
    sorry but i dont know what length of string the user is going to enter, or should i just put a large number e.g. 100? – KAKAK Jul 13 '13 at 17:06
  • 1
    @DeepakTivari: it's very good that you see the problem here. just assuming a maximum input value would make the program vulnerable to buffer overflow shizzle. see http://stackoverflow.com/q/1621394/309483 – Janus Troelsen Jul 13 '13 at 17:08
  • @DeepakTivari Yes, you can just choose a suitably large number. Note that I've also changed the `scanf` call to specify a max size of string to accept to guard against buffer overflow – simonc Jul 13 '13 at 17:09