-2

I am new to writing code, so please briefly explain your answers so I can (try to) keep up, thanks! I am attempting to input a string, assign the string to an array of chars, and to selectively extract a portion of said char string to return (ie input "character" and return "act"), and I cannot seem to understand why I keep getting the error "subscripted value is neither array nor pointer nor vector". Here is my code:

 #include <stdio.h>

    char source[81], result[81];
    int start, count;

    char substring ();

    int main(void) 
    {
        substring ("character", 4, 3, result);
        printf ("%s", result); //print result
        return 0;
    }

    char substring (source, start, count, result)
    {
        int i = 0;
        while (i <= (count-1))
        {
            result[i] = source[((start-1)+i)]; //op chosen chars to result array
            ++i;
            if (i == count)
            {
                result[i] = '\n'; //when i = count, insert null at end of source array
            }
        }
        return result;
    }

When I attemp to compile, I get the error: "

Compilation error    time: 0 memory: 0 signal:0
prog.c: In function ‘substring’:
prog.c:20:13: error: subscripted value is neither array nor pointer nor vector
       result[i] = source[((start-1)+i)]; //op chosen chars to result array
             ^
prog.c:20:25: error: subscripted value is neither array nor pointer nor vector
       result[i] = source[((start-1)+i)]; //op chosen chars to result array
                         ^
prog.c:24:14: error: subscripted value is neither array nor pointer nor vector
        result[i] = '\n'; //when i = count, insert null at end of source array
              ^"
Blade122888
  • 1
  • 1
  • 2
  • and yes, I am aware that to extract 'act' I need to change the '4' argument to '5' or fix the array subset selection condition – Blade122888 Jul 22 '14 at 19:26
  • Your function takes four `int` parameters. If you want to use the global variables, just use them instead of having parameters for them. A better idea would be getting rid of the globals and changing the parameters to have the right type so you can pass the data in. – chris Jul 22 '14 at 19:29
  • The function `char substring (source, start, count, result)` should be something like `char* substring (char *source,int start,int count,char* result)` – francis Jul 22 '14 at 19:29
  • What you have is not valid C code. – The Paramagnetic Croissant Jul 22 '14 at 19:29
  • @TheParamagneticCroissant, Clang gives a nice warning, but only with `-pedantic`: *warning: parameter 'i' was not declared, defaulting to type 'int'* – chris Jul 22 '14 at 19:33
  • @TPC- like I said, I am new to writing code. Put it this way, i'm halfway through the book "Programming in C" by Stephen Kochan, attempting the CH10 exercise 4. Prior to this book I have zero experience in writing code. – Blade122888 Jul 22 '14 at 19:34
  • @chris I take that, but a particular compiler giving or not giving a warning is not a measure of code validity. Only the C standard is. – The Paramagnetic Croissant Jul 22 '14 at 19:34
  • 1
    Ignore the "or vector" part of the message. A "vector" in this context is not a C++ `std::vector`; it refers to a gcc-specific language extension, [documented here](https://gcc.gnu.org/onlinedocs/gcc-4.9.1/gcc/Vector-Extensions.html). – Keith Thompson Jul 22 '14 at 19:35
  • @TheParamagneticCroissant, According to [this question](http://stackoverflow.com/questions/5885156/no-defined-type-of-a-function-parameter-defaults-to-int-am-i-insane) it's only invalid as of C99. I should have specified that, though. – chris Jul 22 '14 at 19:40
  • @chris Unfortunately, yes. (Please note that when I talk about "the C standard", I usually refer to the latest C standard, which is currently C11.) – The Paramagnetic Croissant Jul 22 '14 at 19:41
  • @TheParamagneticCroissant, Yeah, I do the same for C++, so I don't know what I'm doing. – chris Jul 22 '14 at 19:41
  • gahhh I don't even know what c standard this book is in! (*facepalm*) – Blade122888 Jul 22 '14 at 19:48
  • There's a book telling you to do this? – chris Jul 22 '14 at 19:49
  • Yes, I am trying to teach myself, using the book "Programming in C Revised Edition" by Stephen Kochan. literally Im 'newborn' to coding – Blade122888 Jul 22 '14 at 19:52
  • Ahh, I just found that the next chapter I will be reading is about pointers, so I'm guessing I will soon understand exactly what this error is... (fingers crossed) – Blade122888 Jul 22 '14 at 20:07

1 Answers1

2

You should declare explicit types for the arguments of substring():

char substring(char* source, int start, int count, char* result)
Ranic
  • 486
  • 4
  • 12
  • 2
    The form without explicit types is an obsolescent form of function declaration, in which the types default to `int`. – Keith Thompson Jul 22 '14 at 19:36
  • Also, you *must* prototype the function before calling it. If only the function definition is changed, the code compiles but has undefined behaviour at runtime. – M.M Jul 22 '14 at 22:04