I am new to programming. If it's a dumb question please forgive me for wasting a thread. But I wanna know.

Why is this code not working?

int main() {
    char sndex[6][8];


The Compiler (MingW + DevCPP) is showing this:

In function "int main()"
expected primary-expression befor '{' token
expected ';' before '{' token
expected primary-expression before '{' toke
expected ';' before '{' token

The above-mentioned error is being repeated for all the six statements of the assignment.

kvprajapati commented: Very good! First post and [code] tags. +7

Recommended Answers

All 2 Replies

An array can only be initialized when it is declared. Once it's been declared, you have to store the data using strcpy or some such thing.

/* To initialize during declaration. */
    sndex[6][9] = {"BFPV","CGJKQSXZ",...};

    /* To store during runtime. */
    strncpy( sndex[0], "BFPZ", 8 );

As a word of warning, always give yourself an extra byte of space past the end of your longest string, so that you can save that byte for a null byte. For example, "CGJKQSXZ" is actually 9 bytes large, so I had to increase the size of the array by 1 in order to fit it.

Of course, that's is the C style of making string arrays. Since you're using C++, you may be better off creating an array of String objects.

#include <string>

int main( )
    string sndex[6];

    sndex[0] = "BFPV";
    sndex[1] = "CGJKQSXZ";

Use whichever you prefer.

Thanks a lot for this answer. I had a confusion about this for a long time.

Later I solved it using this method.

I created a class which has a constructor that takes inputs for initializing it's objects. This simply

sndex snd[5] = {"String 1", "String 2", ...... , "String 5" };

I just love this automation. Anyway, I later had to implement your method for doing it in C.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.