0

Is it possible to declare an array of pointers and later on initialize either of them and assign a value, in a C header file?

char *i[2];  
i[0] = "abc";

the following does not work.

char *x = "def"; // this will, of course.

How am I supposed to declare and assign values for an array of pointers?

1
  • 1
    Which book on C are you reading? Commented Jun 1, 2011 at 20:25

4 Answers 4

2

This has nothing to do with header files. You cannot create a .c file and put in it code like this:

char *i[2];  
i[0] = "abc";

In C, all code except definitions and initialisations must be inside functions, and your second statement is neither of these - it is an assignment.

An initialisation for your array would look like this:

char *i[2] = {"foo","bar"};

And that could be put in a header file, but would cause multiple definition errors if the header were used in more than one translation unit.

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

Comments

0

It's technically possible. But you should never declare variables on header files. Do it on .c files.

Also, you should initialize char * vectors with something like this:

char *i[2] = { "ABC", "CDE" };

6 Comments

@Neil: It's technically possible to declare variables in header files.
@Pablo Blame the OP's formatting and my bad eyesight. I've deleted the comment.
If by constants you mean #define macros, then you can declare them on header files. But keep in mind they're just macros.
i consider const char pointers also constants. is there any other way of separately instantiating the members of this pointer array, i mean, is it only possible via direct assigment on declaration? thing is, my strings are too large..
@XXL: you can write an init function to initialize your arrays. Otherwise, it's only possible via direct assignment on declaration. There is no executable code outside functions in C.
|
0

Is this what you're looking for?

char *i[2] = {"abc", "def"};

Though it'll give you a warning unless you make them const char *

const char *i[2] = {"abc", "def"};

Comments

0

Are you sure your code

char *i[2];  
i[0] = "abc";

doesn't works?? could you post the errors?

I tried this in gcc and vc++ and it works just fine.

You can also go through the tutorial at this link: http://ee.hawaii.edu/~tep/EE160/Book/chap9/section2.1.4.html

Excerpts:

char * nameptr[MAX];

The array, nameptr[], is an array of size MAX, and each element of the array is a character pointer. It is then possible to assign character pointer values to the elements of the array; for example:

nameptr[i] = "John Smith";

The string "John Smith" is placed somewhere in memory by the compiler and the pointer to the string constant is then assigned to nameptr[i].

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.