Using strcpy with a string array in C - c

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]);

Related

C - I can't get my stringcopy and stringcat function to work on a 2d array

I'm trying to combine a char type 2d array with a string (specifically "*") and a number which is already in the form of a string.
Let's say I have a list of numbers (that have a string datatype) on a 2d array called bingo char bingo[n][n], I then wanted to copy this to a new 2d array called char finalbingo[n][n] along with a "*" with a stringcopy and strcat function.
(e.g: bingo[0][0] contain a string "5", this will become "*5" on finalbingo)
Here is my code:
int l,m,q;
char number;
char finalbingo[n][n];
for (l = 0; l < n; l++){
for (m = 0; m < n; m++){
number = bingo[l][m];
strcpy(&finalbingo[l][m], "*");
strcat(&finalbingo[l][m], &number);
}
}
printf("%c", finalbingo[0][0]);
It does not seem to be working and I'm not sure why.
Thanks for taking your time to help me.

copy two arrays of int to one char* in C

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';

Access violation with strcat()

In my code i have a for loop to concat char by char from another array starting from a certain point. So for example if the text is "hi i need help" and my starting index is 10 it would concat "help" to the end of char* rs.
for (int l = start_index; l < strlen(text); l++) {
strcat(rs, "h");
}
I tested the above and it works fine with no errors, but this
for (int l = start_index; l < strlen(text); l++) {
strcat(rs, text[l]);
}
does not. According to the debugger "text" is char[256] and "rs" is a *char if that helps. The debugger also shows text[l] is of the correct value in that enumeration.
The strcat function takes two strings (pointers to char) as arguments. From the context it seems that text[l] is a single char which can't be used.
One way to work around that is to create a small one-character temporary string for the character:
for (int l = start_index; l < strlen(text); l++) {
char temp[] = { text[l], '\0' }; // Don't forget the terminator
strcat(rs, temp);
}
Another solution is to not use strcat at all, and instead use indexing of the rs string to assign directly to the elements of the string:
size_t end_of_rs = strlen(rs);
for (int l = start_index; l < strlen(text); l++) {
rs[end_of_rs++] = text[l];
}
rs[end_of_rs] = '\0'; // Make sure string is terminated
Or a much simpler solution: Concatenate directly from text:
strcat(rs, &text[start_index]);
But for this you should make sure that start_index isn't beyond the end of text (i.e. you need to verify that start_index < strlen(text)).

append array stored a many ch to to a char array[]

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.

c,Finding length of pointer array

I am trying to find length of pointer array.
Here is my code:
char **histargv[histsize];
histargv[hist] = (char**)malloc(sizeof(char *)*arg);
variable arg is not constant. Each index of histargv array has different arg value.
So how can I find arg value for each index?
Thanks
If the pointers are for strings, then you can figure out the length of the strings (not the size of the memory block) by using strlen. I.e. with:
for (size_t i = 0; i < 100; ++i) {
histargv[i] = read_some_string_somehow_and_return_in_new_buffer();
}
you can print the string including their lengths like this:
for (size_t i = 0; i < 100; ++i) {
printf("%3zu %zu %s\n", i, strlen(histargv[i]), histargv[i]);
}
If this are not strings, then you should follow the link in Eli Sadoffs comment.

Resources