简体   繁体   中英

Using character vs. pointer for an "array of words"

Let's say I have a paragraph and I want to split up all the words and put them in an array. What would be a better way to do it (for this example, let's assume 100 words all under length 20chars):

# character array
char our_array[100][20];
strcpy(our_array[0], "Hello";
strcpy(our_array[1], "Something");

Or:

# string (pointer) array
char *newer_string[100];
newer_string[0] = "Hello";
newer_string[1] = "Something";

Why would one be preferable over the other? And is one more common in practice than the other?

Version 2 does not occupy all the memory at once (as Version 1 would). Also the length of your strings can be arbitrary, you are not bound to a specific length (eg 20). You may get a small memory overhead in Version 2 (due to the pointers you need to save), but this is only really true if at least nearly all words are used and nearly all of them have the specified length.

In general, I would always recommend Version 2 (Array of strings/char pointers). It is even easier to replace strings in this 1D-Array than in the 2D Version.

It depends on what you want to do with that variable. This is definitely not written in stone, and there are little guidelines.

The first option...

char our_array[100][20];
strcpy(our_array[0], "Hello";
strcpy(our_array[1], "Something");

...has the advantage that each element of our_array is actually an array of char . So you can modify that data. Those strings are not read-only.

On the other hand, you are limited to strings of 19 characteres, and it's quite easy to fumble that. Because you are using strcpy() to initialize that array of strings, any error you make will not be detected by the compiler.

The other option...

char *newer_string[100];
newer_string[0] = "Hello";
newer_string[1] = "Something";

...has the advantage that each element of the array is a pointer. The strings are kept in read-only, static storage. They ocupy less space, and you can easy change newer_string[i] to point to something else. However, you cannot modify that data.

Option 1 will assign a fixed 2D array of 100*20. In this case, the strings are stored in the 2D array. This has the following features.

  • Fixed storage per string. If eg one name is 50 characters long, the array needs to be 100*50.
  • The array is mutable. ie you can change the elements easily.
  • No requirement of heap memory allocation ( malloc/calloc )
  • If there is a need of sorting the names, this method requires copying the whole strings around and is inefficient.

Option 2, as you have shown only works for constant string allocations at compile time. If you want to read the string from a file or from the user you need to dynamically allocate the memory. Something like shown below.

char *newer_string[100];
char stringtemp[101];   // size this to the maximum string len you need to support.
int len;
for (i=0; i<100; i++)
{
    scanf("%s",stringtemp);
    len = strlen(stringtemp);
    newer_string[i] = malloc(len+1);
    if (newer_string[i] == NULL) { /*handle memory error*/ }
    strcpy(newer_string[i], stringtemp);
}

The features here are

  • More effecient memory storage. eg if one string is long, only that array element has more memory
  • Needs dynamic memory allocation. So you also need to take care of free
  • Easier to sort. For a sorting algorithm, you need to only swap the pointers newer_string[i] and newer_string[i+1]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM