I have a char array with a size of 17. I need to copy each block of 4 characters from the array into 4 separate char arrays. I'm not exactly how to go about this.

Dani AI

Generated

Nice progress, — and good call to think about assembly early. pointed to a library copy routine and sketched a manual loop; below are practical, assembly-minded pointers that add to those suggestions without repeating their code.

Keep it simple for assembly

  • If you only need read access to four-byte slices, avoid copying at all: store four pointers/offsets into the original buffer and use them directly. That yields only address arithmetic in assembly.
  • If you must copy, prefer a fixed-size byte copy primitive (faster and clearer than conditional string helpers). See the standard semantics for memcpy for predictable behavior: memcpy documentation.
  • Be explicit about handling leftover bytes when the input length is not a multiple of four: either pad, store a length for the last chunk, or zero the remaining bytes before treating the block as a string.

Other low-level notes

  • Use unsigned byte types (uint8_t or unsigned char) for bitwise/byte logic so sign-extension surprises do not appear in assembly.
  • For speed and simpler assembly, consider copying/processing one machine word at a time (e.g., 32-bit loads/stores) but account for alignment and endianness.
  • Avoid strncpy for fixed-byte copying; its null-padding behavior is surprising for many use cases (see strncpy notes).

Testing checklist before porting

  • Test boundary lengths (0..n), unaligned buffers, and different endianness if applicable.
  • Keep the C loop structure flat: a single loop that advances a base pointer by four bytes maps cleanly to assembly.

Recommended Answers

All 4 Replies

you can use strncpy() strncpy(a2, strings+4, 4); or you can just write a function that would copy 4 bytes from the starting address.
The latter is better, I think

I like the function better. I'm having a bit of a mind block about though. I know I have to iterate through arr[17]. But how do I stop at the 4th element, switch to a new array and read the next 4 chars?

I like the function better. I'm having a bit of a mind block about though. I know I have to iterate through arr[17]. But how do I stop at the 4th element, switch to a new array and read the next 4 chars?

Well I respect your zeal to write your own functions but what PRABAKAR said is absolutely right.You can use that standard function given in the library.(Standard functions are given so that programmers can code good programs without getting bogged down by certain stuff.)

If you want to code it your self do this:

//Declare four character arrays or an array as arr[4][5] ie four char //arrays to hold four characters and a '\0'

//Declare an array to take up your input string as str[17] or //something

//Take your input
i=0;
a=0;
while(i < strlen[str])
{
          for(j=0;j<4;)
                    arr[a][j++] = str[i++];
 
          arr[a][j]='\0';
          a++;          
}

The code is self explanatory. I have padded the arr array's 5th cell ie

arr[a][4] = '\0';

so that next time while printing you can just give

printf("%s",arr[a]);

Hope you can code as you wanted now.But I still recommend usage of standard functions when they are possible.

I wanted to say thanks for everyones help. Its been a little bit since a programmed c++ and a long time since c. I dare say they are one of the same but each with their own nuance and personality. I couldn't do another function because eventually this program will be converted to assembly. So the easier my c code, the easier my assembly. After I got my head in the game this is what I came up with:

#include "stdio.h"

int main()
{
	unsigned char array[17] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', '\0'};

	int i;
	char key1[5];
	char key2[5];
	char key3[5];
	char key4[5];
	
	for(i = 0; i < 4; i++)
	{
		key1[i] = array[i];
	}
	for(i = 4; i < 8; i++)
	{
		key2[i - 4] = array[i];
	}
	for(i = 8; i < 12; i++)
	{
		key3[i -8] = array[i];
	}
	for(i = 12; i < 17; i++)
	{
		key4[i - 12] = array[i];
	}

	key1[4] = '\0';
	key2[4] = '\0';
	key3[4] = '\0';
	key4[4] = '\0';

	for(i = 0; i < 4; i++)
	{
		printf("Key 1 = %c \n", key1[i]);
	}

	for(i = 0; i < 4; i++)
	{
		printf("Key 2 = %c \n", key2[i]);
	}
	
	for(i = 0; i < 4; i++)
	{
		printf("Key 3 = %c \n", key3[i]);
	}
	
	for(i = 0; i < 4; i++)
	{
		printf("Key 4 = %c \n", key4[i]);
	}
	
	return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.