array of pointers to bit arrays - c

I want to make an array of pointers to bit arrays. I make this func2 to test the pointers, but I get a seg fault when I try to acess an elemeny of the bit array outside the function. What am I doing wrong?
int func2(int i, int* bit_array){
int j;
for(j = 0; j< i; j++)
bit_array[j] = malloc(sizeof(int) * i);
for(j = 0; j< i; j++)
bit_array[j] = 0;
return 1;
}
int main(){
int** bit_root;
bit_root = malloc(sizeof(int *) * 5);
func2(5, bit_root);
int n;
for(n = 0; n < 5; n++)
printf("%d ", bit_root[0][n]); //error
printf("\n");
return 0;
}

You are sending the array incorrect to the function func2. func2 need to be:
int func2(int i, int** bit_array){
int j,k;
for(j = 0; j< i; j++)
bit_array[j] = malloc(sizeof(int) * i);
for(j = 0; j< i; j++)
for(k = 0; k< i; k++)
bit_array[j][k] = 0;
return 1;
}
int main(){
int** bit_root;
bit_root = malloc(sizeof(int *) * 5);
func2(5, bit_root);
int n;
for(n = 0; n < 5; n++)
printf("%d ", bit_root[0][n]); //error
printf("\n");
return 0;
}

In the lines below you allocate memory for array of int for each element of bit_array and assign pointers to int arrays to bit_array elements:
for(j = 0; j< i; j++)
bit_array[j] = malloc(sizeof(int) * i);
But here you assign zeroes to bit_array elements. Thus you rewrite pointers to zero as if you didn't allocate memore at all:
for(j = 0; j< i; j++)
bit_array[j] = 0;
To fix it replace the this last block this a following code:
int k;
for(j = 0; j< i; j++)
for(k = 0; k < i; k++)
bit_array[j][k] = 0;
Here in the first loop you iterate through the array of pointers to int arrays (bit_array[j]) and in the inner loop you iterate through the array of ints (bit_array[j][k]). These changes requires changing of func2 definition - second parameter must be pointer to pointer to int instead of just a pointer. It helps you to get rid from warnings of compiler.
To see what is going on clearly you can use following code:
int k, *int_array = NULL;
for(j = 0; j< i; j++)
{
int_array = bit_array[j]; // get the pointer to int array
for(k = 0; k < i; k++)
int_array[k] = 0; // assign values to int array
}
And don't forget to free all these memory for both inner arrays and bit_array.

Related

Re loading integers into an array

The code below works fine when M <= 4, but seems to bugger up if you put in a whole number that's any bigger (in this case, I actually need M to be 10). Does anyone know why this is happening and what can be done about it? Thanks.
/*
"Read all 100 numbers from the text file and store it in a 10x10 array."
*/
#include <stdio.h>
#include <stdlib.h>
FILE *fptr;
int n;
int M = 4; // Length and width of array
int main()
{
// Allocating memory
int **myArray = (int **)malloc(M * sizeof(int));
for (int j = 0; j < M; j++) {
myArray[j] = (int *)malloc(M * sizeof(int));
}
// Loading data into array
fptr = fopen("List of Numbers.txt","r");
for (int i = 0; i < M; i++) {
for (int j = 0; j < M; j++) {
fscanf(fptr,"%d",&n);
// printf("%d ",n);
myArray[i][j] = n;
}
}
fclose(fptr);
// Printing
for (int i = 0; i < M; i++) {
for(int j = 0; j < M; j++) {
printf("%d ",myArray[i][j]);
}
printf("\n");
}
return 0;
}
This line:
int **myArray = (int **)malloc(M * sizeof(int));
should be:
int **myArray = (int **)malloc(M * sizeof(int *));
^
You are allocating an array of pointers, not an array of ints.

Matrix Multiplication function won't compile inside the main

This is a simple matrix multiplication, code won't compile. I also want to take the function outside. I know I have to have global variables and function declaration, but the code won't even compile inside the main.
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define N 10
float *A[N], *B[N], *C[N];
int main(){
int count = 0, i,j, k;
for (i = 0; i < N; i++)
A[i] = (float*)malloc(N * sizeof(float));
for (i = 0; i < N; i++)
B[i] = (float*)malloc(N * sizeof(float));
for (i = 0; i < N; i++)
C[i] = (float*)malloc(N * sizeof(float));
for (i = 0; i < N; i++)
for (j = 0; j < N; j++){
A[i][j] = ++count;
B[i][j] = count;
}
void multiply(float* A, float* B, int n) {
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
for(int k=0; k<n; k++)
C[i][j] += A[i][k] * B[k][j];
}
for (i = 0; i < N; i++)
for (j = 0; j < N; j++)
printf("%f\t", C[i][j] );
}
Your multiply function definition inside main makes it a nested function which is not allowed in C. You can call as many functions inside a function but cannot define a new function inside an already existing one.
Plus you have also not called the multiply function.
A , B and C are defined at the top as global as an array of pointers to float
inside your multiply function you have locally redefined them as a pointer to a float..
this is why the multiplication fails inside multiply.
you are pretending that A and B are arrays of pointers to float again.. and they have been locally scoped as pointers to float.
Does this do what you were expecting ?
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define N 10
float *A[N], *B[N], *C[N];
void multiply( float *a[], float *b[], float *c[], int n )
{
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
for(int k=0; k<n; k++)
c[i][j] += (a[i][k]) * b[k][j];
}
int main(){
int count = 0, i,j, k;
for (i = 0; i < N; i++)
A[i] = (float*)malloc(N * sizeof(float));
for (i = 0; i < N; i++)
B[i] = (float*)malloc(N * sizeof(float));
for (i = 0; i < N; i++)
C[i] = (float*)malloc(N * sizeof(float));
for (i = 0; i < N; i++)
for (j = 0; j < N; j++)
{
A[i][j] = ++count;
B[i][j] = count;
}
multiply(A,B, C, N);
for (i = 0; i < N; i++)
for (j = 0; j < N; j++)
printf("%f\t", C[i][j] );
}
if so please explain it back to me how this works

Output the values of a matrix by passing its pointer to a function

I am trying to send a pointer of a matrix to function for printing the values. However, my following code prints some long numbers so I assumed it prints the addresses instead! How can I print the value of the matrix after passing the pointer of it?
#include <stdio.h>
#include <string.h>
#include <math.h>
void printing(int *edge);
void main(){
int N=3;
int i,j;
int *edge[N];
for (i = 0; i < N; i++){
*(edge+i) = (int *)malloc(N * sizeof(int));
}
srand(0);
for(i = 0; i < N; i++){
for(j = 0; j < N; j++){
if(i == j)
*(*(edge+i)+j) = 0;
else
*(*(edge+i)+j) = 1; //rand() % 10;
}
}
printing(edge); // Pass the pointer of the matrix
}
void printing(int *edge){
int i,j;
int N= 3;
for( i = 0; i < N; i++){
for(j = 0; j < N; j++){
printf("%d \t", ((edge+i)+j)); // I think I have to do something in this line.
}
printf("\n");
}
}
The parameter type of printing is incorrect. It should be int *edge[]. Then when you print, use *(*(edge+i)+j), or better yet edge[i][j].
The result:
void printing(int *edge[]){
int i,j;
int N = 3;
for( i = 0; i < N; i++){
for(j = 0; j < N; j++){
printf("%d \t", edge[i][j]);
}
printf("\n");
}
}
Also, be sure to #include <stdlib.h>, as it's needed for malloc and srand.

C - How to adapt this code for a very large/dynamic array to a 2 dimensional array?

int main()
{
double *array;
long int n;
n=10000000;//10^7
array = (double *) malloc(n*sizeof(double));
return 0;
}
basically, I want to use this code for a really big aray into a 2 dimensional array, which will have dimensions [very large][4].
If you want a 2D array, then allocate a 2D array. It's that simple.
double (*pArr)[4] = malloc(10000000 * sizeof pArr[0]);
Notes:
do not cast the return value of malloc().
use sizeof pArr[0] instead of sizeof(TheDataType) for defensive programming reasons.
This seems working on Wandbox.
#include <stdio.h>
#include <stdlib.h>
int main(void) {
double (* array)[4];
long int n;
int i, j;
n=10000000;//10^7
array = (double (*)[4]) malloc(n*sizeof(double[4]));
printf("%u\n",(unsigned int)sizeof(array[0]));
printf("%u\n",(unsigned int)sizeof(double[4]));
for (i = 0; i <n; i++) {
for (j = 0; j < 4; j++) array[i][j] = (double)i * j;
}
for (i = 0; i < 10; i++) {
for (j = 0; j < 4; j++) printf("%f ", array[i][j]);
putchar('\n');
}
for (i = n - 10; i < n; i++) {
for (j = 0; j < 4; j++) printf("%f ", array[i][j]);
putchar('\n');
}
free(array);
return 0;
}
int n = 100000;
double** array = malloc(sizeof(double*)*n);
for (int i = 0; i < n; ++i)
{
array[i] = malloc(4*sizeof(double));
}
Also note that we don't cast the malloc's result(Do I cast the result of malloc?).

Use Jagged Array In c

How to Insert and then print data from jagged array in below code ?
int *jagged[5];
jagged[0] = malloc(sizeof(int) * 10);
You can insert by adding a second subscript for the nested array's index.
int i;
for (i = 0; i < 10; ++i)
jagged[0][i] = some_value;
and print like
int i;
for (i = 0; i < 10; ++i)
printf("%d\n", jagged[0][i]);
Keep in mind that you need to keep track of each nested array's length on your own. Depending on your needs, you might do something like
int jagged_lengths[] = {10, 5, 4, 0, 3};
int i, j;
// Write some data
for (i = 0; i < 5; ++i) {
jagged[i] = malloc(sizeof(int) * jagged_lengths[i]);
for (j = 0; j < jagged_lengths[i]; ++j)
jagged[i][j] = some_value;
}
// Read back the data
for (i = 0; i < 5; ++i)
for (j = 0; j < jagged_lengths[i]; ++j)
printf("%d\n", jagged[i][j]);
first of all, why not define your array as a multi dimentional array? unless you want the size of each member different, you don't need to use malloc for each member, simply do:
int jagged[5][10];
as for iterating, you can do something like:
int i,j;
for (i = 0; i < 5; i++)
for (j = 0; j < 10; j++)
jagged[i][j] = i*j; //or any value you want
for (i = 0; i < 5; i++)
for (j = 0; j < 10; j++)
printf ("%d,%d: %d\n", i, j, jagged[i][j]);

Resources