0
votes

im trying to reverse a string but im running into a problem where it says "subscripted value is neither array nor pointer nor vector" pointing to char holder = input[i]; can someone help explain that to me

myreverse(input, rev, len)
{
  int i = 0;
  int j= len -1;
  char string[len];

  while(i<j)
  {
      char holder = input[i];
      string[i]= string[j];
      string[j] = holder;
      i++;
      j--;
  }
}


int main(int argc, char* argv[]) {
  if (argc < 2) {
    printf("Usage: %s <word to reverse>\n", argv[0]);
    exit(1);
  }
  char* input = argv[1];
  int len = strlen(input);
  char rev[len + 1]; // Adding one for the null terminator
  myreverse(input, rev, len);
  printf("Rev string is %s\n", rev);
}
3
Don't use K&R (pre-standard) C function declarations. You omitted the types in the myreverse() function, so the return type and the argument types are all assumed to be int, and you can't subscript int. It should be void myreverse(char *input, char *rev, int len) or thereabouts. You don't use the rev parameter in the code; that's bad too. - Jonathan Leffler
@JonathanLeffler Bonus: Trying to use implicit int in combination with VLAs (char string[len]), which are a C99 feature. - melpomene
@melpomene, why is it a "bonus"? Both are perfectly legal. If you're looking to blame something, blame C for accepting input this lax. - zneak
@zneak Not together. C99 added VLAs, but removed implicit int from the language. - melpomene
I believe that the point stands that if there's someone to blame, it's not the new developer trying to figure out 45 years of baggage. - zneak

3 Answers

1
votes

Parameter types default to int in C, so you need to specify them explicitly for your function since they're char pointers:

void myreverse(const char* input, char* rev, int len)

Also, your function should write its result in rev, not in its local variable string.

1
votes

You code is almost fine. But myreverse() doesn't do anything useful, since it reverses a local (uninitialized) string. Probably an oversight on your part.

I don't really agree with the way you allocate the reversed string in main. Using strdup(), then freeing the memory would be better, and more portable. But if your compiler allows it...

Why don't you reverse the string in place ?

You should consider this:

void myreverse(char* s)
{
  int j;
  int i = 0;
  if (s == NULL)
      return;

  j = strlen(s) - 1;
  while(i < j)
  {
      char t = s[i];
      s[i]= s[j];
      s[j] = t;
      i++;
      j--;
  }
}
0
votes

You should find a bit more up to date C book first. This function definitions are from the K&R era. Here you have the reverse string function. You do need to provide size, as all C strings are zero terminated

char *reverse(char *str)
{
    char tmp;
    int len;

    if (str != NULL)
    {
        len = strlen(str);
        for (int i = 0; i < len / 2; i++)
        {
            tmp = *(str + i);
            *(str + i) = *(str + len - i - 1);
            *(str + len - i - 1) = tmp;

        }
    }
    return str;
}

Or a different version

char *reversendup(consr char *srcstr, char *rev, size_t bufsize)
{
    int len;
    char *wrk;

    if (str != NULL)
    {
        len = strlen(str);
        if ((wrk = calloc(sizeof(char), len + 1)) != NULL)
        {
            for (int i = 0; i < len; i++)
            {
                *(wrk + i) = *(srcstr + len - i - 1);
            }
            strncpy(rev, wrk, bufsize);
            free(wrk);
        }
    }
    return rev;
}