+ 1

How to split char* into 5 parts

Hi I have been provided a char* pointer char* p and populated for sure. It is sure that it has six parts as below: 1. First part is 1 byte and we can assume that it is L1. so, subsequent L1 bytes are of first portion of data (assume data1). 2. Next 1 byte represent L2 and subsequent L2 bytes are of second portion of data (assume data2). 3. Next 1 byte again represent L3 and subsequent L3 bytes are of data(assume data3) Now, query is how to populate data1,data2 and data3 bytes from char* p to new memory char* p1,char* p2 and char* p3?

10th Feb 2025, 12:49 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
14 ответов
+ 2
I am sorry for my misunderstanding. Scratch all that I said about this being RLE! This is about parsing a string of variable length fields. Allow me to regroup my thoughts on this. In this light I might consider using sprintf with variable width format specifiers, or else maybe simple strncpy(). Using sprintf I think you can do such parsing in only one line of code. I will try it and see.
10th Feb 2025, 5:30 PM
Brian
Brian - avatar
+ 2
What I have proposed by using snprintf is not very fast. If you need ultimate speed and use the least memory, then I could propose another idea. That is to destructively replace each field width value within the original string with NULL, and store *data1, *data2, and *data3 as pointers to the beginning of each field after each NULL.
10th Feb 2025, 6:20 PM
Brian
Brian - avatar
+ 1
Ketan Lalcheta see if this code is closer to your need. I am using snprintf, which limits the number of characters that it copies from the original string. This looks a little messy, but it demonstrates the concept. I will leave it up to you to write it cleaner, as I am trying not to write it all for you. You could use strncpy just as well. char L1, L2, L3; char data1[257], data2[257], data3[257]; L1 = *p; snprintf(data1, L1, "%s", p+1); L2 = *(p+L1+1); snprintf(data2, L2, "%s", p+L1+2); L3 = *(p+L1+L2+2); snprintf(data2, L3, "%s", p+L1+L2+3); puts(data1); puts(data2); puts(data3);
10th Feb 2025, 5:56 PM
Brian
Brian - avatar
0
Ketan Lalcheta this sounds like run-length encoding (RLE), which is a simple compression scheme. Aren't steps 1, 2 and 3 repetition of the same procedure, or am I reading this wrong? In C there are not many choices to efficiently fill a string with a repeated character. Consider using memset(). It's an easy 1-liner and generally the fastest way to bulk fill with a given value. Edit: I just confirmed that the optimizer will likely implement memset as an inline single instruction for small run length values like this. Be sure to pass a char type for L1, L2, L3 so that it can know you are working with small values.
10th Feb 2025, 3:21 PM
Brian
Brian - avatar
0
I am sorry but I don't understand rle so not sure whether it is same or different. This was discussed during our office tea time and I over heard it. It step 1 , 2 and 3 are same as you found it right. However, char* ptr has three such steps and all these data are accommodated in a single char pointer.
10th Feb 2025, 3:27 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
I googled it as term rle you had provided. It seems I am not looking for it. What rle is for confirming my understanding : WWWEYY means W4EY2 This is not I am looking for. I have char* pointer and want to decode this input pointer to find three data parts available inside char*
10th Feb 2025, 3:30 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
What I meant to say was that your task is to decode data using an RLE scheme (not to encode it). In concept, this is what I understand: L1 = *p C1 = *(p+1) L2 = *(p+2) C2 = *(p+3) L3 = *(p+4) C3 = *(p+5) My suggestion is to use memset to fill an output char array with the number of repeated characters L1, L2, L3 given as C1, C2, C3. For output my concept is something like: char out[257] = {0}; memset(out, L1, C1); printf(out); memset(out, L2, C2); out[L2] = 0; printf(out); memset(out, L3, C3); out[L3] = 0; printf(out);
10th Feb 2025, 4:09 PM
Brian
Brian - avatar
0
Why you did +1 to all the increments ? I thought char moves 1 bytes and should be fine if we get c1 = p+1 But why l2 is moved 2 bytes from p? C1 is more than 1 bytes. Right?
10th Feb 2025, 5:04 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
Also what is the meaning of the one byte? Does it store any int value? If not int value, how we can allocate C1 as size of char array with length of L1
10th Feb 2025, 5:06 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
I appreciate your help Brian One more query i have as below: 1. L1 is char but can it be a int (no. As it is 1 byte only.) So, char* data1 = new char[L1] is not an option here. What does this physical interpretation mean ? What can be hold by L1 and data1 and so on. I am not able to follow importance of this. I know I heard this question, but curious to know what is this mean ? Again thank you so much for this.
10th Feb 2025, 6:15 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
But it is still spinning my head Brian We do pointer arithmetic on numbers. Like p+1 or p+4 or p++ or p-- or p-4 You had L1 as char and then doing L2 = (p+L1+1) . Here pointer arithmetic is used on the int , char and pointer as l1 is char.
10th Feb 2025, 7:16 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
L1 would be a 1-byte int that can hold values 0-255. In char type C does not distinguish between character and integer or even fixed point decimals. It is simply a value in memory, and char type means it is 8 bits of storage. It can be used to represent 8 bits of whatever you need, and is not necessarily a printable character. Your tag in the post indicated a topic in C, so I am avoiding mixing C++ syntax here. You breached that by mentioning "new char[L1]" 🙂. For speed in C, I shun dynamic memory allocation and instead recommend pre-allocating the max size of data strings if they are not too large. In this case it is very reasonable. Since L1 holds values 0-255, the max data array length is 256+1 bytes (add 1 to include terminating NULL). After extracting the strings from *p there may be unused space in data1 array past the terminating null, but it is efficient. Did I address your questions appropriately? I'll be glad to clarify further.
10th Feb 2025, 7:30 PM
Brian
Brian - avatar
0
Maybe a little remediation on pointers will help. Note when I assign the values of L1, L2, L3 that I used pointer dereferencing operator * to extract the byte value from the memory location. The pointer p holds the memory address. The dereference *p gives the char (i.e., byte) value that is stored at memory address p. Using p+L1+1 is a calculated memory address offset from p. The dereference *(p+L1+1) gives the char value stored at memory address (p+L1+1)
10th Feb 2025, 7:41 PM
Brian
Brian - avatar
0
Thanks Brian I learnt something new today. I never thought char can be passed into a pointer arithmetic. I was stuck to get L1 by doing L1-'0' to get int out of it and was confused that I would not get L1 into int format as it is not of 4 bytes. With no availability of int size, I was stuck for doing arithmetic to reach to data part. Seems I never came across the scenario of such kind and now a new learning . Thank you so much.
11th Feb 2025, 2:02 AM
Ketan Lalcheta
Ketan Lalcheta - avatar