0

My problem is as follows: I have text file from which I read all the contents into a single string. I now need to make an array of strings that divide the original string into equally sized chunks. I will be performing some operations on each of those chunks, but I'm having a hard time understanding how arrays of strings work in C.

In the code below, I seem to be able to achieve the splitting of my original array.

The text file is as follows:

0123456791
abcedfghij
thirt

I then put into a char array called buffer and do the following:


int n = 9;
int chunk = strlen(buffer)/division; // answer is 3
char array_chunks[9][3];
for (int i = 0; i < n; i++){
    
    {
    int starting_point = i*chunk;
    memcpy(array_chunks[i],(buffer+starting_point), chunk); 
   // buffer has 27 characters in this example
    printf(":%s\n", array_chunks[i]);
    }
}


which gives me:

012
345
679
1
a
bcd
efg
hij

th
irt

This is what I want as it gives me the original string in blocks of 3 (including the newline, which is fine for what I want to do later). However, when I once again try to print each index of the array of strings, I get the following:

for (int i =0 ; i<n; i++){
int a = strlen(array_chunks[i]);
printf("Len: %d\n", a);
printf("Chunk:%s\n", array_chunks[i]);}
Len: 27
Chunk:0123456791
abcdefghij
thirt
Len: 24
Chunk:3456791
abcdefghij
thirt
Len: 21
Chunk:6791
abcdefghij
thirt
Len: 18
Chunk:1
abcdefghij
thirt
Len: 15
Chunk:bcdefghij
thirt
Len: 12
Chunk:efghij
thirt
Len: 9
Chunk:hij
thirt
Len: 6
Chunk:
thirt
Len: 3
Chunk:irt
Len: 27
Chunk:0123456791
abcdefghij
thirt
Len: 24
Chunk:3456791
abcdefghij
thirt
Len: 21
Chunk:6791
abcdefghij
thirt
Len: 18
Chunk:1
abcdefghij
thirt
Len: 15
Chunk:bcdefghij
thirt
Len: 12
Chunk:efghij
thirt
Len: 9
Chunk:hij
thirt
Len: 6
Chunk:
thirt
Len: 3
Chunk:irt

What I want to do is call a function on n (in this case 9) character arrays each of the size of the chunk (in this case 3) but after printing each char array it seems like it wasn't partitioned how I intended, or how it was shown to me in the first for loop. When I then go onto use these chunks I get segmentation faults. I assume I'm misunderstanding how an array of strings in C works, but I couldn't really find much information on what I'm trying to accomplish. Any help is appreciated

3
  • 3
    Strings need a null byte to terminate them. You've neither allocated the space for the null bytes nor assigned null bytes to terminate the strings. Commented Dec 26, 2022 at 14:56
  • I see. I'm relatively new to C so I wasn't aware/didn't remember that detail. I suppose I should assign the array as: array_chunk = [9][4]? And how would I assign the null bytes? Commented Dec 26, 2022 at 15:08
  • Yes, you need char array_chunk[9][4];. The simplest way to assign the null byte is array_chunk[i][3] = '\0'; after the memcpy(). Commented Dec 26, 2022 at 15:17

1 Answer 1

2

Thanks to Jonathan Leffler for the solution. I had to allocate space for and then assign a null byte to each char array like so:

int n = 9;
int chunk = strlen(buffer) / division; // answer is 3
char array_chunks[9][4];
for (int i = 0; i < n; i++) {
  {
    int starting_point = i * chunk;
    memcpy(array_chunks[i], (buffer + starting_point), chunk);
    // buffer has 27 characters in this example
    array_chunk[i][3] = '\0';
    printf(":%s\n", array_chunks[i]);
  }
}
Sign up to request clarification or add additional context in comments.

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.