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.
Related
Apologies if this is simple, but I am new to C. I am trying to create a loop that fills in an empty array of strings with multiple strings. However, at the end, the whole array is being filled with the latest element ! Below is the code:
int main(void)
{
string array_test[2];
char string_test[300];
for (int i = 0; i < 2; i++)
{
snprintf(string_test, sizeof(string_test),"Test: %i", i);
array_test[i] = string_test;
}
for (int i = 0; i < 2; i++)
{
printf("%s\n", array_test[i]);
}
}
This returns:
Test: 1
Test: 1
But I am expecting:
Test: 0
Test: 1
Because you are using the same buffer to save strings in all iterations. This will make previous strings overwritten by new strings.
Allocate separate buffers for each strings to avoid this.
/* put #include of required headers here */
int main(void)
{
string array_test[2];
char string_test[2][300];
for (int i = 0; i < 2; i++)
{
snprintf(string_test[i], sizeof(string_test[i]),"Test: %i", i);
array_test[i] = string_test[i];
}
for (int i = 0; i < 2; i++)
{
printf("%s\n", array_test[i]);
}
}
Why the for loop is filling the whole array with the latest string?
The for loop is filling the whole array of pointers array_test with the address of the first character of the character array string_test.
That is you declared an array of two pointers
string array_test[2];
and each element of the array points to the first character of the same array string_test
array_test[i] = string_test;
The statement above is equivalent to the following statement
array_test[i] = &string_test[0];
That is an array designator used in expressions with rare exceptions is converted to a pointer to its first element.
So you are outputting the same character array string_test using two pointers.
printf("%s\n", array_test[i]);
Instead of the array of pointers you could declare a two-dimensional character array like
char array_test[2][300];
and in the first for loop you could copy strings formed in the array string_test into elements of the array array_test like
strcpy( array_test[i], string_test );
In this case each element of the two-dimensional array will store its own string.
All elements in the string array point to the same buffer, so they all appear to have the same string, more precisely the value last composed into this buffer.
Using the typedef string for char * creates confusion about this fact, which is one more reason to not hide pointers behind typedefs.
You can allocate a copy of the string in the loop:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char *array_test[2];
char string_test[300];
for (int i = 0; i < 2; i++) {
snprintf(string_test, sizeof(string_test), "Test: %i", i);
array_test[i] = strdup(string_test);
}
for (int i = 0; i < 2; i++) {
printf("%s\n", array_test[i]);
}
for (int i = 0; i < 2; i++) {
free(array_test[i]);
}
return 0;
}
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
I'm Trying to check in my matrix of dimension [10][10], which spots are available to store data (String) there and which are occupied.
The code basically goes through the whole matrix and checks every spot.
I have tried using the strlen and != NULL but everything just prints that the spot is free.
char parque[10][10];
for(int i = 0; i < 10; i++) {
for(int j = 0; j < 10; j++) {
parque[i][j] = "";
}
}
parque[5][5]="f47ac10b-58cb-4372-a567-0e02b2c3d499,ANR";
for(int i = 0; i < 10; i++) {
for(int j = 0; j < 10; j++) {
if(parque[i][j] != "") {
printf("The Spot [%d][%d] is taken",i,j);
} else {
printf("The Spot [%d][%d] is free",i,j);
}
}
}
Basically the spot [5][5] should print that it's taken, at least that's what I want it to do...
Thanks in advance!
Your declaration
char parque[10][10];
declares a two-dimensional array of char. If you compile your code with a strict compiler, you'll get an error:
error: assignment makes integer from pointer without a cast [-Wint-conversion]
parque[i][j] = "";
^
What you did mean is to make an array of pointers to const char, like here:
const char* parque[10][10];
Then your program will say that The Spot [5][5] is taken.
You can't use !=. You need to use strcmp. And, of course, you need to initialize your array content before iterating it and using its values to compare with "" string.
This condition:
if(parque[i][j] != "")
Will become:
if (strcmp(parque[i][j], ""))
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.