from now I have a ch[5] stored {'a','b','c','d','e'}
I have another array char loadtext[i];
which will store many strings.;
like loadtext[0] = "abced"
how can append the five char together;
I have tried;
for(i = 0; i < 5; i++){
strcat(loadtext[0],ch[i];
}
but ir return erorrs
[Warning] passing argument 1 of 'strcat' makes pointer from integer without a cast
How can I solve it?
The strcat function is used to copy strings. You're not copying strings but individual characters, so just assign the values directly:
for(i = 0; i < 5; i++){
loadtext[0][i] = ch[i];
}
loadtext[0][5] = '\0';
Note also that we add a null byte to the end of loadtext[0] to make the array of characters a string.
Related
I have to arrays of int for example arr1={0,1,1,0,0}, arr2={1,0,1,1,1} and I need to return 1 char* created by malloc that will be shown like this : "01100,10111".
when I do for loop it doesn't work, how can I do it ?
char* ans = (char*)malloc((size * 2+1) * sizeof(int));
for (int i = 0; i < size; i++)
ans[i] = first[i];
ans[size] = ",";
for (int i = size+1; i < 2*size+1; i++)
ans[i] = second[i];
Among the multitude of problems:
Your allocation size is wrong. It should include space for the separating comma and the terminating nullchar. sizeof(int) is wrong regardless, it should be sizeof(char) and as-such can be omitted (sizeof(char) is always 1).
Your storage is wrong. You want to store characters, and your values should be adjusted relative to '0'.
Your indexing of the second loop is wrong.
In reality, you don't need the second loop in the first place:
char* ans = malloc(size * 2 + 2);
for (int i = 0; i < size; i++)
{
ans[i] = '0' + first[i];
ans[size+1+i] = '0' + second[i];
}
ans[size] = ',';
ans[2*size+1] = 0;
That's it.
1.
char* ans = (char*)malloc((size * 2+1) * sizeof(int));
What is size here? It is not defined and declared in the provided code.
You do not need to cast the return value of malloc() to char. In fact, you do not need to cast the return value of malloc() anymore. It is a habit from the early C days.
Why do you need a char pointer here at all exactly? If you want to print 01100,10111 there is no need to use a char pointer for the output of the integer values.
2.
for (int i = 0; i < size; i++)
ans[i] = first[i];
Again what is size here?
What is first here? If it isn´t a pointer this statement is invalid.
3.
ans[size] = ",";
This operation is invalid. You are trying to assign a string to a pointer.
By the way, I don´t know what you trying to do with this statement. You can incorporate the comma separate in the output of 01100,10111, without your intend to include it int the memory of the int arrays itself.
4.
for (int i = size+1; i < 2*size+1; i++)
ans[i] = second[i];
Same as above: What is value and the type of size?
What is second? If it isn´t it a pointer this statement is invalid.
5.
To answer to the question title:
(How to) Copy two arrays of int to one char* in C
This isn´t possible. You can´t copy two arrays with its data to a pointer to char.
There are at least four issues with your code.
You malloc the wrong size, you want to use sizeof(char).
You need to zero terminate it, so you need to add extra room for the terminating zero
char* ans = (char*)malloc((size * 2+2) * sizeof(char));
second[size * 2+1] = 0;
Also the indexing of the second loop is wrong. You are accessing second array out of bounds. Make the loop more like the first.
We also need to convert the integer value to a char in the loops.
for (int i = 0; i < size; i++)
ans[size+i+1] = second[i] + '0';
I have a source pointer (pSource) and a goal pointer (pGoal). I also have a number of characters (n) that need to be copied to the pGoal from pSource.
I thought that I can just copy what's in the pSource to pGoal and move both pointers to the next location. (Both are pointing at the start at the beginning).
for (int i = 0; i < n; i++) {
pGoal+i = pSource+i;
}
Assuming that your pointers are of type char *, the correct way of doing this is:
for (int i = 0; i < n; i++) {
*(pGoal+i) = *(pSource+i);
// or pGoal[i] = pSource[i]
}
You can also check memcpy
for(i = 0; i < sizeof(buff); i++) if(buff[i] = ' ') ++num;
char **parts[num];
char *p;
p = strtok(buff, " ");
int j = 0;
while(p != NULL)
{
parts[j] = p;
j++;
p = strtok(NULL, " ");
}
During compiling the warning was raised:
assignment from incompatible pointer type, where parts[j] = p is.
What can I do?
I am assuming that you would like to store individual tokens in an array. As is currently declared, parts is an array of pointers to pointers. That is not what you want for an array of C strings - you need to remove one asterisk:
char *parts[num]; // This requires at least C99
Note that num needs to start at 1, not at 0, because the number of tokens that you are going to get is one greater than the number of spaces that you will find in the string.
Once you do, your program would still need some fixing in the way it deals with results of strtok: you should not store results for use after the next call of strtok, because these results become invalid. Instead, you should make duplicates of tokens before storing them in the parts array:
parts[j] = strdup(p);
char *parts[num];
you can declare a pointer to the array of pointers declared above like so:
char *(*ptr)[num] = &parts;
but you don't need this extra level of indirection to store an array of strings
Do you use this line in your actual code?
for(i = 0; i < sizeof(buff); i++) if(buff[i] = ' ') ++num;
You are setting entire buffer to spaces. And setting num to sizeof(buff)
So, in other words this might not position you where you want:
char **parts[num];
I have a few questions regarding array of strings in C.
I have an array char *string. I have a char *string and then I split every 4 characters in a array of strings called sep_str. So for example if char *string = 'The sum';, then char **sep_str is:
0: |_| --> "The "
1: |_| --> "Sum"
My first question is, in an array of strings in C (so array of array of chars), will there be a null terminating character at the end of each sep_str[i], or just at the last position of sep_str? Here is how I copy string into an array of strings:
for (int i = 0; i < str_length; i++) {
sep_str[i/4][i%4] = *ptr;
ptr++;
}
My second question is, how would I reverse the elements of each string in sep_str? Here's how I did it, but I feel like it is stepping out of the array of the substring. (so out of the element of the sep_str):
// Reverse each element in the array
char temp;
for (int i = 0; i < num_strs; i++) {
for (int j = 0, k = 4; j < k; j++, k--) {
temp = sep_str[i][j];
sep_str[i][j] = sep_str[i][k];
sep_str[i][k] = temp;
}
}
The copy of the strings sounds good to me. Since each string has always 4 chars, you can avoid the null terminator \0. Alternatively you need to declare sep_str as a 5x(lenght/4) matrix, to store the \0 char at the end of each string.
To reverse a string you need to iterate from the start to the middle of the string, replacing the i-th char with the length-i-1-th. You need to replace the inner for replacing k=3 to k=2.
You also need to take care of the last string, since the lenght might not be multiple of four.
char temp;
for (int i = 0; i < (num_strs - 1); i++) {
for (int j = 0, k = 3; j < k; j++, k--) {
temp = sep_str[i][j];
sep_str[i][j] = sep_str[i][k];
sep_str[i][k] = temp;
}
}
if (num_strs > 0) {
for (int j = 0, k = strlen(sep_str[i]) - 1; j < k; j++, k--) {
temp = sep_str[i][j];
sep_str[i][j] = sep_str[i][k];
sep_str[i][k] = temp;
}
}
In a C string, there will be only one termination character. But if you need to tokenize the strings, then each string must be null terminated.
But before that -
char *string = "The sum"; // should be const char* string = "The sum";
String literal in the above case resides in read only location and cannot be modified. If you need to modify, then
char string[] = "The sum";
If you don't have the terminating character in your strings then yes, you will be outside the bounds of the array since you are accessing sep_str[i][4], which is not a valid location:
sep_str[0] = 'T'
sep_str[1] = 'h'
sep_str[2] = 'e'
sep_str[3] = ' '
However, I doubt that you want to have the null character at the beginning of your string, so you need k=3 in your for loop, not k=4.
My first question is, in an array of strings in C (so array of array of chars), will there be a null terminating character at the end of each sep_str[i], or just at the last position of sep_str?
Only at the end, but if you want to treat each individual chunk as its own string, you'll need to add the \0 yourself.
My second question is, how would I reverse the elements of each string in sep_str?
You could do it with pointers...
char temp;
// Point to start of string, `str` will decay to first memory position.
char *start = str;
// Point to the end of the string. You will need to `#include <string.h>`
// for `strlen()`. Otherwise, write a `while` loop that goes until `\0` to find
// the last position.
char *end = &str[strlen(str) - 1];
// Do until we hit the middle of the string.
while (start < end) {
// Need a temp char, no parallel assignment in C.
temp = str[start];
// Swap chars.
str[start++] = str[end];
str[end--] = str[temp];
}
Assuming str is your string.
I have a character array defined as follows: char *c[20];
I'm trying to do: strcpy(c[k], "undefined); but it's not working
I've also tried defining it as char c[20][70] with no luck.
Edit: I actually know it's an array of character arrays, I need it like that.
That's not a character array; that's an array of character pointers. Remove the * to make it a character array:
char c[20];
Then you can use strcpy:
strcpy(c, "undefined");
If you really did want an array of character arrays, you'll have to do what you said you tried:
// array that holds 20 arrays that can hold up to 70 chars each
char c[20][70];
// copy "undefined" into the third element
strcpy(c[2], "undefined");
The problem could have been you're missing the closing ", I don't know if that was a paste error though. Or, the problem could have been that you're using k without defining it, we can't know without seeing the error message you get.
If you want to set them all to that string, then just loop over them:
char c[20][70];
int i;
for (i = 0; i < 20; ++i)
strcpy(c[i], "undefined");
If what you want is to have 20 strings of 70 chars each then your second option should work:
char c[20][70];
for (int k = 0; k < 20; k++)
strcpy(c[k], "undefined");
The char *c[20] definition is incorrect because you are just defining an array of 20 pointers, and the pointers are not initialized. You could make it work in this way:
char *c[20];
for (int k = 0; k < 20; k++) {
c[k] = malloc(70);
strcpy(c[k], "undefined");
}
// then when you are done with these strings
for (int k = 0; k < 20; k++)
free(c[k]);