3

I have char * lines[1000] string that can hold 1000 characters. How to create 100 arrays of that string. I get error with this code down.

char * lines[1000];
lines = (lines*)malloc(100 * sizeof(lines));

main.c:19:20: error: expected expression before ')' token

2
  • 1
    It should be a cast to (char*) not (lines*). Commented Oct 29, 2011 at 16:06
  • It isn't clear what you want. What it isn clear is that char * lines[1000] isn't a pointer to a string of 1000 characters but is a pointer to a pointer of an array of char. It's a char**. Commented Oct 29, 2011 at 16:07

4 Answers 4

3

The simplest way is:

char lines[100][1000];

Alternatively:

char* lines[100];
int i;
for (i = 0; i < 100; i++) {
  lines[i] = malloc(1000);
}
...
for (i = 0; i < 100; i++) {
  free(lines[i]);
}

The latter is a bit more flexible in that -- with minor modifications -- it permits you to allocate a different amount of memory for every string.

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

4 Comments

Realistically, they always will, but its worth noting that this depends on chars being a single byte.
@Aaron sizeof(char) is by definition equal to 1. Other issue if whether one char value is big enough to store an actual character, using your current encoding.
@rodrigo There's a difference between the implementation you're using and the ANSI C specs. Assuming a char is one byte is, according to specs, not portable.
@Aaron What the ANSI C does not guarantee is that a byte is 8 bits (its CHAR_BIT bits), but that a char is size 1 byte is guaranteed by definition. From C99 specs, 6.5.3.4.3: When [sizeof is] applied to an operand that has type char, unsigned char, or signed char, (or a qualified version thereof) the result is 1.
3

It looks like you want an array strings, each string holding at most 1000 characters.  There are some issues with your code.

  1. You've declared an array of char *s but what you really want is a pointer to an array of chars.  For that, your declaration should be

    char (*lines)[1000];
    
  2. On the other hand, you shouldn't forget about the NULL bytes at the end of strings, and should probably instead declare

    char (*lines)[1001];
    
  3. To set the pointer, you'll want to use

    lines = (char (*)[1001]) malloc(100 * sizeof(char[1001]));
    

    or

    lines = (char (*)[1001]) malloc(100 * sizeof(*lines));
    

    the latter working because, with lines a pointer to an array of chars, *lines is a char[1001].  Remember to make sure you didn't get a NULL pointer back.

  4. At the end, you should free the memory you've malloced with

    free(lines);
    

Comments

1

You can write a for-loop as:

char * lines[1000];

int i = 0;

for (i = 0; i < 1000; i++)
{
    lines[i] = (char*)malloc(100 * sizeof(lines));
}

Don't forget to free-up the memory pointed by all the pointers

for (i = 0; i < 1000; i++)
{
    free(lines[i])
}

1 Comment

While this is what the code in the question implies, it does not seem to be what is actually being asked for (and doesn't make a whole lot of sense - why is the size of a char array dependent on the size of a pointer array?).
0

Why don't you create a 2 dimensional array?

2 Comments

sorry, I wasn't clear. It must be dynamic array of n elements
please edit your question, so future visitors to this question may understand the answer more. :)

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.