-1

Below is my code:

    #include <stdio.h>
    #include <stdlib.h>


    typedef int index_key;  
    typedef char text_t;          

    text_t *create_text()
{
    //text_t text[SIZ];
    text_t *c;
    text_t text[]="fl";
    c= text;
    return c;
}


    int main()
    {

        text_t * create();


        return 0;

    }

I get an error - expected expression before ‘]’ token. Why is this error occuring? Isn't text[] a global declaration and I can access it anywhere? What is wrong in this program and how should I correct it.

1
  • Don't you have to specify the size of the array? and the index? Commented Feb 18, 2015 at 5:46

3 Answers 3

1

You cannot have an array definition like

text_t text[];

Either specify the size,

#define SIZ 256   //arbitary value

text_t text[SIZ];

or use initializer.

text_t text[] = {`A`, `B`, `C`};

EDIT:

As per the latest addition, please be informed that "sldk" (as you've iused) and {'s', 'd', 'l', 'k'} (as i've suggested) are not the same. The former is a string literal while the later being initalizer list of chars. You can use the second in your case, not the first one.


EDIT 2

That said, your create_text() function is wrong. Effectively, you're returning the address of a local variable text. Incorrect. Will invoke UB.

Sign up to request clarification or add additional context in comments.

5 Comments

Hello , If I use text[SIZ] declaration . I get a warning at text[SIZ]="sldk" that assignment makes integer from pointer without a cast[enabled by default]. Since pointer *c is already of type character , what is there to case here?
@Arnold Hint: "sldk" is not of type char, right?
@Arnold Also, while initializing, you need not provide the array size explicitly. Leave it to the compiler, it'll do the job for you. For example, see the last line in my answer. Is it clear now? :-)
Ok , what does assignment make integer from pointer means ? "sldk" is not a char but its not integer either,
@Arnold make integer from pointer , yes "sdlk" effectively gives a pointer to the starting address of the string literal
1

I see the following problems:

text_t text[];

is a declaration, not a definition. You have to add a line that defines text, such as:

text_t text[100];

The line

text[]="sldk";

is wrong on two accounts.

  1. You cannot assign to an array using the = operator.

  2. You cannot use text[] to access the array.

Here's a fixed version of your program.

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

typedef int index_key;  
typedef char text_t;

// This is a forward declaration of the array.
text_t text[];

text_t *create()
{
    text_t *c;

    // One way to get some string into a char array.
    strcpy(text,"sldk");

    c = text;
    return c;
}

// This is the definition of the array.
text_t text[100];

int main()
{
    text_t * create();
    return 0;
}

Update

Regarding your updated create_text function...

It should work since string literals are stored in read-only memory of the program. It will lead to problems as soon as you try to change anything in it. It's better to use a safer approach, such as:

text_t *create_text()
{
    text_t *c = malloc(20);
    strcpy(c, "fl");
    return c;
}

1 Comment

Hello , Good explanation.thanks. You said I can not assign it to array using = operator . How about if I remove the global definition and use text_t *create_text() { //text_t text[SIZ]; text_t *c; text_t text[]="fl"; c= text; return c; }
0

The C language does not have a built in string data type unlike C++, Java or C#. The strings are represented as arrays of characters in C. So any array in C should have some size.

int numbers[10];
char string[50];

The C function which operate on these types of strings ( like strlen which calculates the length of the strings expects a 'null' or '\0' character at the end of the array.

Initializing a character array with string literal like "test" will automatically insert a null character at the end.

char str[10] = "test";
characters stored {'t','e','s','t','\0'}

If you are initializing it with comma separated characters, you need to explicitly specify this character.

char str[10] = {'t','e','s','t', '\0'}; //explicit null character

So, generally the array size is one more than the maximum size of the string you want to store. So it is quite common to see declaration like the following.

char name[MAXLEN+1];

If you are using C++, you can use a build-in data type called string.

string str;

Hope this helps

Comments

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.