Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I am facing issue copying data to void pointer array through for loop. I am not allowed to use double pointer.
I am trying someting like this:
for(i=0;i<9;i++){
array + i = i;
}
but I cant just do this, and I know that,
I am trying to do it like this
memcpy(
array+i,
i,
s
);
I have already allocated memory:
My actual code is some thing like this:
if(v->e_type==V_INT){
// printf("%p ",v->e_array+v->no_e);
memcpy(
v->e_array+v->no_e,
new_val,
v->e_sz
);
}
If you have something like this:
int *x = malloc(sizeof(int)*4);
You can put values in it with pointer arithmetic:
for(int i = 0; i < 4; i++) {
*(x+i) = i; // or *(x+1) = 0; for example
}
Or simply:
for(int i = 0; i < 4; i++) {
x[i] = i;
}
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
Im trying to define and initialize an array of strings using a function, but, the function is causing segmentation fault while allocating memory for each of the pointers.
Please find below the minimal reproducible version of the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define COL 100
#define ROW 10
int init_arr(char ***arr_ptr) {
*arr_ptr = malloc(sizeof(char *) * ROW);
for(int temp_iter = 0; temp_iter < ROW; ++temp_iter) {
*arr_ptr[temp_iter] = malloc(COL + 1);
strncpy(*arr_ptr[temp_iter], "MY_STRING_IS_THIS", COL);
}
return 0;
}
int main() {
char **arr_of_str = NULL;
init_arr(&arr_of_str);
for(int temp_iter = 0; temp_iter < ROW; ++temp_iter) {
printf("\nData: %s", arr_of_str[temp_iter]);
}
}
This code works fine when the function is split into 2 where the first function defines it and 2nd one initializes it.
The problem lies here *arr_ptr[temp_iter], first dereference(in his case a subscription) that was applied was from box brackets, and then from asterix. What you want to do is separate these dereferences, so that asterix is applied first and the from box brackets, like this:
int init_arr(char*** arr_ptr) {
*arr_ptr = (char**) malloc(sizeof(char *) * ROW);
for(int temp_iter = 0; temp_iter < ROW; ++temp_iter) {
(*arr_ptr)[temp_iter] = (char*) malloc(COL + 1);
strncpy((*arr_ptr)[temp_iter], "MY_STRING_IS_THIS", COL);
}
return 0;
}
Also, you need to cast the pointer from malloc into appropriate one, since the malloc always returns void*, and you are dereferencing void pointer later on.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I would like to create an integer list in C containing integer arrays of variable size. The length of the main list will not need to change.
How would I declare it - my implementation - particularly my access is not working:
int dataArray[length];
int dataArray[0] = [1,2,3];
int dataArray[1] = [5,6];
.
.
.
populate
for(int a = 0; a<sizeof(dataArray); a++) {
tempArr = dataArray[a];
for(into = 0; b>sizeof(tempArr); b++) {
print(dataArray[a][b])
}
}
Your main array needs to hold pointers to other arrays. Therefore it should not be int dataArray[length] but rather int* dataArray[length] this means it will hold length amount of references to integer arrays.
int* array[length];
int randomSizeArray[x];
randomSizeArray[0] = 1;
.
.
.
randomSizeArray[x] = 5;
int* array[0] = randomSizeArray;
Also sizeof() will not work the way you expect it to - in C you need to store separately how many elements are in an array. I'd recommend reading a C tutorial from the ground up as you seem to have shaky knowledge of basics.
The sizeof results in the byte count not element count.
Divide the array size by the element size to find the element count.
int dataArray[length];
int dataArray[0] = [1,2,3];
int dataArray[1] = [5,6];
...
// for(int a = 0; a<sizeof(dataArray); a++) {
for(size_t a = 0; a<sizeof(dataArray)/sizeof(dataArray[0]); a++) {
tempArr = dataArray[a];
// for(into = 0; b>sizeof(tempArr); b++) {
for(into = 0; b>sizeof(tempArr)/sizeof(tempArr[0]); b++) {
// or do you really want
// for(size_t = 0; b<sizeof(tempArr)/sizeof(tempArr[0]); b++) {
print(dataArray[a][b])
}
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
So I'm calling a simple method from a header file to the main C file and it's not working. Any clue?
Main
#include "header.h"
int main() {
int a = 2;
int b = 5;
int numArray[2] = {a, b};
displayNumbers(numArray, 2);
doubleIt(a);
doubleIt(b);
displayNumbers(numArray, 2);
return(0);
}
Header
int doubleIt(int x) {
return 2 * x;
}
void displayNumbers(int x[], int numSize) {
int i;
for (i = 0; i < numSize - 1; i++) {
printf("%d, ", x[i]));
printf("%d", x[numSize - 1]);
printf("\n");
}
The doubleIt method doesn't work.
If i understand your question properly, you want to double the array and print the values, in your case if a = 2 and b = 5 then you want to double to a = 4 and b = 10.
Modify these lines in your code as follows;
numArray[0] = doubleIt(a);
numArray[1] = doubleIt(b);
Hope this helps.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have here some sample code that has what I think is needed to solve my problem.
Basicaly I need to store inside an array that has a variable amount of entries a variable amount of reasons for it. Is this array[5].b[3] not allowed? Is there any alternative for it?
Thanks in advance
typedef struct {
int a;
char *reasons;
}t_a;
f() {
int space=10,spaceReasons=5;
t_a *array;
array=NULL;
array=realloc(array,sizeof(t_a)*space);
array[5].reasons=realloc(array[5].reasons,sizeof(char)*spaceReasons);
fgets (array[5].reasons[3]),300, stdin);
free(array);
}
Here's a working example:
#include <stdlib.h>
#include <stdio.h>
typedef struct {
int a;
char* reasons;
} t_a;
int main(void) {
int space = 10, spaceReasons = 5;
t_a* array = NULL;
array = realloc(array, sizeof(t_a) * space);
for (int i = 0; i < space; i++) {
array[i].reasons = NULL;
array[i].reasons = realloc(array[i].reasons, sizeof(char) * spaceReasons);
// fill with fgets
fgets (array[i].reasons, spaceReasons, stdin);
}
// print results
for (int i = 0; i < space; i++) {
printf("%s\n", array[i].reasons);
}
// first free all reasons
for (int i = 0; i < space; i++) {
free(array[i].reasons);
}
// then free array
free(array);
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am working on a lab and I need some help!
Lets say I have a 3 dimensional array double a[2][3][4]. I want to update the values of inside this array through void functions. How would I set that up? I am having trouble initializing a pointer to the double that I want send in as an argument. I want to do something to every value inside of the 3d array so all 24 values?
You probably want this:
void MyFunction (double a[2][3][4])
{
int i,j,k ;
for (i = 0; i < 2; i++)
for (j = 0; j < 3; j++)
for (k = 0; k < 4; k++)
{
a[i][j][k] = 5.0 ;
}
}
void main()
{
double a[2][3][4] ;
MyFunction(a) ;
// now every element of array a contains 5.0
}