1
void reverses(char s[])
{
    int i, count;
    i = count = 0;

    // Get the total character count in array
    while (s[count] != '\0') ++count;

    char copy[count];

Char array copy is only being defined when an integer is being used directly, such as char copy[15]; or char copy[DEFINED];.

I'm passing it an integer via the int count;, which has a declared value, so why isn't my array being defined?

I found an error in my code after that which is copied here, sorry.

I was using a while loop to do the reversing-- changing that to a for loop fixed the problem.

Many of your answers were very helpful to me anyway. So, Thank You David Cullen, Vlad from Moscos, R Sahu, user0815, and haccks

Vlad's answer is fantastic, thanks for enlightening me-- I will give much thought to your answer. David Cullen's exactly followed my logic and gets the correct answer!

5
  • 1
    If you are using gcc, you can use -std=c99 to enable variable length arrays. Commented Mar 27, 2015 at 21:22
  • this line: 'while (s[count] != '\0') ++count;' should probably be: 'count = strlen(s);' Commented Mar 27, 2015 at 21:23
  • 1
    this line: 'char copy[count];' needs to be 1 byte longer to allow for the NUL string termination byte: 'char copy[count+1];' Commented Mar 27, 2015 at 21:25
  • 1
    How exactly does it not work? If the compiler printed an error message, please include it in your question. Commented Mar 27, 2015 at 21:35
  • after this function finishes its' work, how is the rest of the code expected to be able to access the reversed string? Note: to keep it a string, there needs to be a trailing '\0' that will need to be appended by the code (or initialize the variable to all '\0' via char copy[count+1] = {'\0'}; Commented Mar 27, 2015 at 21:37

5 Answers 5

3

This code produced the expected output:

#include <stdio.h>

void reverses(char s[])
{
    int i, count;
    i = count = 0;

    while (s[count] != '\0') ++count;

    printf("count = %d\n", count);

    char copy[count + 1];

    for (i = 0; i < count; i++) {
        copy[count - i - 1] = s[i];
    }
    copy[count] = '\0';

    printf("copy = %s\n", copy);
}

int main(void)
{
    reverses("string");
}

Output:

count = 6
copy = gnirts

This was tested with gcc on OS X:

Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 6.0 (clang-600.0.57) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin13.4.0
Thread model: posix
Sign up to request clarification or add additional context in comments.

4 Comments

This won't compile using a compiler that doesn't support C99 and that seems to be the problem.
I was confident that the problem was with the compiler, but I wanted to post a working example. If the OP compiles the working example with his compiler and everything works, then he knows that he has another problem.
@David Cullen You forgot to add terminating zero to array copy.
@VladfromMoscow Should be fixed now
2

It seems that your compiler does not support variable length arrays.

Take into account that to write function reverse there is no need to define an auxiliary array. The function can be written the following way

char * reverse( char s[] )
{
    size_t i = 0, n = 0;

    while ( s[n] ) ++n;

    for ( ; i < n / 2; i++ )
    {
        char c = s[i];
        s[i] = s[n-i-1];
        s[n-i-1] = c;
    }

    return s;
}

4 Comments

most likely, the first statement is wrong. but the following statement and code are good
there is a problem, with this code. The array is a literal in readonly memory, so it cannot be changed. That is why the copy[] array is being generated
the C compilers, for some 30 years, have allowed array to be defined using a variable for the length of the array. The only constraint is the variable (in this case char copy[] ) must be declared immediately after a open brace '{'
@user3629249 Where do you see using of string literal in my post? And secondly for example MS VS does not support yet C99.
0

Variable length arrays is one of the feature added to C99. You need to compile your code with flag -std=c99 in GCC.

You should also note, if you are using MSVS, that MSVS does not support VLAs.

Comments

0

The proper way to implement variable length arrays in C is with malloc.

#include <stdlib.h>

void makeArrayOfSize(int size) {
   // Create memory for array
   char *chars = (char *)malloc(size * sizeof(char));

   // Do business logic

   free(chars); // ALWAYS free pointers to allocated memory to avoid leaks
}

Though, you can make a string reverser without copying the string (IF it is not hard-coded to the string table)...

void reverse(char* string) {
   int length = 0;
   int index = 0;
   char temp;

   // Ensure string is not null
   if (!string) {
      return;
   }

   // Get length assuming string has proper '\0' char at the end
   while (string[length]) {
      ++length;
   }

   // Play catch!
   while (index < length / 2) {
      temp = string[index++];
      string[index - 1] = string[length - index];
      string[length - index] = temp;
   }
}

Disclaimer: I didn't compile or run this code... but it reminded me of an old homework assignment so I found it nostalgic to give it a shot :)

Edit: Compiled this code. Works perfectly unless the string is hard-coded in the string table (will produce a bus error). Also will not crash if string is empty.

Took longer to write a good test than the code but here's the test and the output showing working dynamic data, but an error on the hard string:

int main(int argc, char** arcv) {
   char *string;
   char string0[0];
   char string1[1];
   char string2[2];
   char string3[3];
   char string4[4];
   char string5[5];
   char string6[6];
   char *string7 = "abcdef";

   string2[0] = 'a';

   string3[0] = 'a';
   string3[1] = 'b';

   string4[0] = 'a';
   string4[1] = 'b';
   string4[2] = 'c';

   string5[0] = 'a';
   string5[1] = 'b';
   string5[2] = 'c';
   string5[3] = 'd';

   string6[0] = 'a';
   string6[1] = 'b';
   string6[2] = 'c';
   string6[3] = 'd';
   string6[4] = 'e';

   printf("String: %s\n", string);
   reverse(string);
   printf("Reverse String: %s\n", string);

   printf("String0: %s\n", string0);
   reverse(string0);
   printf("Reverse String0: %s\n", string0);

   printf("String1: %s\n", string1);
   reverse(string1);
   printf("Reverse String1: %s\n", string1);

   printf("String2: %s\n", string2);
   reverse(string2);
   printf("Reverse String2: %s\n", string2);

   printf("String3: %s\n", string3);
   reverse(string3);
   printf("Reverse String3: %s\n", string3);

   printf("String4: %s\n", string4);
   reverse(string4);
   printf("Reverse String4: %s\n", string4);

   printf("String5: %s\n", string5);
   reverse(string5);
   printf("Reverse String5: %s\n", string5);

   printf("String6: %s\n", string6);
   reverse(string6);
   printf("Reverse String6: %s\n", string6);

   printf("String7: %s\n", string7);
   printf("(error after this)\n");
   reverse(string7);
   printf("Reverse String7: %s\n", string7);

   return 0;
}

Output:

String: (null)
Reverse String: (null)
String0: 
Reverse String0: 
String1: 
Reverse String1: 
String2: a
Reverse String2: a
String3: ab
Reverse String3: ba
String4: abc
Reverse String4: cba
String5: abcd
Reverse String5: dcba
String6: abcde
Reverse String6: edcba
String7: abcdef
(error after this)
Bus error: 10

2 Comments

The "Play catch!" part is fantastic, very well thought through! Thanks for sharing so much!
Glad you appreciated the effort :) Though vlad gave the same solution in less time than it took me to write the test driver. In all honesty, malloc/free is the proper dynamic array declaration in C.
-1

in earlier versions of C, a variable could only be defined immediately after an open brace '{'

so enclose the variable definition and the associated/following code in braces.

I.E.

{
    char copy[count+1];
    ..... // other associated code that uses the 'copy' variable
}

1 Comment

It seems unlikely that any compiler would support VLA but not support declarations after statements

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.