1

I have been using Coderbyte.com the last couple of days for some coding challenges in C. I normally use Codeblocks as my IDE, I have noticed that sometimes the working solution in the Coderbyte IDE will throw an error in Codeblocks. For example:

#include <stdio.h>
#include <string.h>

void AlphabetSoup(char str[]) {
    int i, j, length;

    length = strlen(str);

    char new_string[length];
    char temp;

    strcpy(new_string, str);

    for (i = 0; i < length; i++) {
        for (j = i + 1; j < length; j++) {
            if (new_string[i] > new_string[j]) {
                temp = new_string[i];
                new_string[i] = new_string[j];
                new_string[j] = temp;
            }
        }
    }

    // code goes here
    printf("%s", new_string);
}

int main(void) {
    AlphabetSoup(gets(stdin));
    return 0;
}

In Codeblocks it is throwing at error in the main function saying:

warning: passing argument 1 of 'gets' from incompatible pointer type [enabled by default]

anyways I don't understand why this solution is working on one IDE and not the other. Another time some code I put in said that it would only work in C99.

Now when I run this code in Codeblocks it crashes, but not on Coderbyte.

My questions are:

1) Are there different versions of C?

2) Is this code still correct, or would it be better to use char * for the function parameter

I'm still new to C

chqrlie
  • 131,814
  • 10
  • 121
  • 189
RobotMan
  • 605
  • 1
  • 5
  • 10
  • 4
    [**Never use `gets()`!**](https://stackoverflow.com/q/35358767/10077) – Fred Larson Apr 30 '19 at 15:47
  • 2
    `gets` require a string buffer, it also doesn’t accept a stream, only `fgets` accepts a file stream. – vandench Apr 30 '19 at 15:47
  • 2
    !) Yes, definitely. 2) `gets` [expects a different argument](http://www.cplusplus.com/reference/cstdio/gets/). – ForceBru Apr 30 '19 at 15:47
  • A couple of points: 1) "warning" != "error", 2) different compilers can emit different messages. This can usually be controlled with a compiler switch (e.g. `gcc -Wall -pedantic`), 3) [gets() is Evil](https://stackoverflow.com/questions/1694036/). Don't use it :) Prefer [fgets()](https://linux.die.net/man/3/fgets) (among other alternatives). – paulsm4 Apr 30 '19 at 15:48
  • @paulsm4 C is a pretty loose language so a lot of the time it will produce warnings instead of formal errors, the errors will probably start showing up in the linking stage. – vandench Apr 30 '19 at 15:50
  • 3
    You need it to be `char new_string[length + 1];`. Otherwise, you write one past the end of the buffer with `strcpy`. – Millie Smith Apr 30 '19 at 15:56
  • 1
    is it difficult to to see that gets gets another type parameter? Simple google "gets" gives the answer instantly. – 0___________ Apr 30 '19 at 19:41

2 Answers2

4

1) Are there different versions of C?

Yes, and the specific reason that the validity of this code differs across C standards is that you are using the function gets, which has been deprecated and later fully removed by modern C standards. There is practically no way to use gets in production code without risking a buffer overflow, so it's recommended to use functions that check the length of the buffer. fgets is the most common with:

fgets(buffer, BUFFER_SIZE, stdin);

2) Is this code still correct, or would it be better to use char * for the function parameter

There is no difference between the function parameters: char *foo and char foo[], since when an array is passed as an argument to a function, it decays to a pointer to its first element. Both syntaxes are acceptable.

Govind Parmar
  • 20,656
  • 7
  • 53
  • 85
  • 1
    I would say one of the questions not worth answering. OP was not bothered to take look on the function prototype to see what parameter it takes. BTW on this level of the C knowlegge safety of the code is secondary - it is far too early for the production code – 0___________ Apr 30 '19 at 19:43
-2

This syntax is not correct because the array declaration in c/c++ must have a constant size at the compile time.

length = strlen(str);

char new_string[length];

the length value cannot be determined at the compile time. if you want to control the size at the run time then you have to use malloc in C or the new operator (C++).

See Static array vs. dynamic array in C++

Amr Rady
  • 1,057
  • 1
  • 12
  • 24