1

I am new to C and I have no idea how to handle this array:

char *args[MAX_LINE/2 + 1];

What does this line mean exactly? Is it a pointer to an array of chars? The assignment given with this was to fill this array with multiple string tokens, but I don't understand how a char pointer can store a whole string?

2
  • it's an array of chars, sized to be MAX_LINE/2 + 1, e.g. 50/2 + 1 -> 26. Commented Mar 3, 2014 at 3:48
  • 3
    It is an array of char pointers. The size is MAX_LINE/2 +1 Commented Mar 3, 2014 at 3:50

2 Answers 2

5
char *args[MAX_LINE/2 + 1];

args is an array of pointer to char of size MAX_LINE / 2 + 1. Each element is a char*, i.e., each element may be a string. You'll have to initialize them though (i.e., point them somewhere valid.) For example, to read from stdin:

args[0] = malloc(some_size);
/* read a string from standard input */
fgets(args[0], some_size, stdin);
Sign up to request clarification or add additional context in comments.

5 Comments

... which may be being used as an array of strings; we'd have to see what values are being put into it.
how do I go about storing strings into this array?
@user3373372: Allocate each element with malloc and copy them in, point them to some other string, etc. However you would populate any string in C. Added an example
@Ed S. so I tried args[0] = malloc(70); but the compiler says "incompatible implicit declaration of built-in function âmallocâ". Am I missing something?
@user3373372: #include <stdlib.h>. You need to include the declarations for the functions you use. You'll need stdio.h for fgets. It's all in the documentation.
0

That is basically an array of pointers. Each of the pointer points to a location that holds a char .

Take a look here for more details on handling it. http://www.tutorialspoint.com/cprogramming/c_array_of_pointers.htm

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.