To access any element I use *(Ptr + i).
Is there any way to put 2D array into the allocated memory in order to access any element using array[i][j]?
Here is the code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *Ptr;
Ptr = malloc(M*N*sizeof(int));
for (i = 0; i <= M * N; i++)
*(Ptr + i) = 1 + rand()%10;
return 0;
}
Sample
#include <stdio.h>
#include <stdlib.h>
#define N 4
#define M 3
int main()
{
int *Ptr;
int (*p)[M];
int i,j;
Ptr = malloc(M*N*sizeof(int));
for (i = 0; i < M * N; i++){
*(Ptr + i) = 1 + rand()%10;
// printf("%d ", Ptr[i]);
}
// printf("\n");
p=(int (*)[M])Ptr;//p[N][M]
for(i = 0; i < N ;++i){
for(j = 0; j < M;++j)
printf("%d ", p[i][j]);
printf("\n");
}
return 0;
}
Try this:
int** theArray;
theArray = (int**) malloc(M*sizeof(int*));
for (int i = 0; i < M; i++)
{
theArray[i] = (int*) malloc(N*sizeof(int));
}
Sample:
int M = 5;
int N = 5;
int** theArray = (int**) malloc(M*sizeof(int*));
for (int i = 0; i < M; i++)
{
theArray[i] = (int*) malloc(N*sizeof(int));
for(int j = 0 ; j < N; j++)
{
theArray[i][j] = i+j;
printf("%d ", theArray[i][j]);
}
printf("\n");
}
for (int k = 0; k < M; k++)
{
free(theArray[k]);
}
free(theArray);
Related
I'm trying to write a code in C that sum two 4x4 matrix.
But I want my function to have a pointer as my arguments. The only error I'm getting is the time I'm trying to sum up in the function. Could someone help me?
#include <stdio.h>
#include <locale.h>
int i = 0, j = 0;
void calc_soma(int* mat_A, int* mat_B, int* mat_C)
{
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
mat_C[i][j] = mat_A[i][j] + mat_B[i][j];
printf("%d", mat_C[i][j]);
}
}
}
int main()
{
setlocale(LC_ALL, "Portuguese");
int i=0, j=0;
int mA[4][4], mB[4][4], mC[4][4];
int *mat_A, *mat_B, *mat_C;
for(i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
printf("Type in the value for Matrix A [%d][%d]: ", i, j);
scanf_s("%d", &mA[i][j]);
}
}
i, j = 0;
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
printf("Type in the value for Matrix B [%d][%d]: ", i, j);
scanf_s("%d", &mB[i][j]);
}
}
*mat_A = &mA;
*mat_B = &mB;
return 0;
}
The types of pointers for the arguments are wrong. You want to pass (the pointer to the first elements of) arrays like int mA[4][4];, so they should be pointers to int[4].
void calc_soma(int (*mat_A)[4], int (*mat_B)[4], int (*mat_C)[4])
{
/* same as original */
}
They can simply be written like this:
void calc_soma(int mat_A[][4], int mat_B[][4], int mat_C[][4])
{
/* same as original */
}
Then the function can be called like:
calc_soma(mA, mB, mC);
The purpose of mat_A and mat_B are unclear, but if you want to get pointers to the matrice like &mA, it should be int(*)[4][4]. Note that dereferencing (like *mat_A) uninitialized pointers will invoke undefined behavior.
int main()
{
setlocale(LC_ALL, "Portuguese");
int i=0, j=0;
int mA[4][4], mB[4][4], mC[4][4];
int (*mat_A)[4][4], (*mat_B)[4][4], (*mat_C)[4][4];
/* omit */
mat_A = &mA;
mat_B = &mB;
return 0;
}
To use functions like
void calc_soma(int* mat_A, int* mat_B, int* mat_C)
you should express the matrice by 1D array to match with the format. It will be like this:
#include <stdio.h>
#include <locale.h>
#define ROWS 4
#define COLS 4
int i = 0, j = 0;
void calc_soma(int* mat_A, int* mat_B, int* mat_C)
{
for (i = 0; i < ROWS; i++)
{
for (j = 0; j < COLS; j++)
{
mat_C[i * COLS + j] = mat_A[i * COLS + j] + mat_B[i * COLS + j];
printf("%d", mat_C[i * COLS + j]);
}
}
}
int main()
{
setlocale(LC_ALL, "Portuguese");
int i=0, j=0;
int mA[ROWS * COLS], mB[ROWS * COLS], mC[ROWS * COLS];
for(i = 0; i < ROWS; i++)
{
for (j = 0; j < COLS; j++)
{
printf("Type in the value for Matrix A [%d][%d]: ", i, j);
scanf_s("%d", &mA[i * COLS + j]);
}
}
i, j = 0;
for (i = 0; i < ROWS; i++)
{
for (j = 0; j < COLS; j++)
{
printf("Type in the value for Matrix B [%d][%d]: ", i, j);
scanf_s("%d", &mB[i * COLS + j]);
}
}
calc_soma(mA, mB, mC);
return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(){
int N;
scanf("%d", &N);
int **arr;
arr = (int**)malloc(sizeof(int) * N);
for(int i = 0; i < N; i++){
arr[i] = (int*)malloc(sizeof(int) * N);
}
for(int i = 0; i < N; i++){
for(int j = 0; j < N; j++){
if(i == 0 || j == 0)
arr[i][j] = 1;
else
arr[i][j] = arr[i - 1][j] + arr[i][j - 1];
}
}
for(int i = 0; i < N; i++){
for(int j = 0; j < N; j++){
printf("%d ", arr[i][j]);
}
printf("\n");
}
for(int i = 0; i < N; i++)
free(arr[i]);
free(arr);
return 0;
}
This code makes a 2-dimensional array with a pointer; I want assign value arr[i][j] = 1. However, an error EXC_BAD_ACCESS occurs in XCode on a Mac. I don't know how to solve the problem. What is my mistake? The assigning for loop is where the trouble occurs.
Can't increase rows in 2d array, but columns is ok.
#include <stdio.h>
#include <stdlib.h>
it is working:
void increasecolumn(int ** mas, int* n, int m){
for (int i = 0; i < m; i++){
int* tmp = realloc(mas[i], sizeof (*mas[i]) * ((*n) + 1));
if (tmp){
mas[i] = tmp;
}
}
(*n) = (*n) + 1;
}
but increasing rows failed
void increaserow(int ** mas, int n, int* m){
int ** tmp = realloc(mas, sizeof(*mas) * ((*m) + 1));
if (tmp){
mas = tmp;
for (int i = 0; i < 1; i++){
mas[(*m) + i] = malloc(sizeof(*mas[(*m) + i]) * n);
}
}
(*m) = (*m) + 1;
}
int main(int argc, char * argv[]) {
int n = 3; // columns
int m = 2; // rows
int** mas = malloc(m*sizeof(*mas));
for(int i = 0; i < m; i++){
mas[i] = malloc(n*sizeof(*(mas[i])));
}
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
mas[i][j] = 0;
printf("%d ", mas[i][j]);
}
printf("\n");
}
printf("\n");
increasecolumn(mas, &n, m);
for (int i = 0; i < m; i++){
mas[i][n-1] = 1;
}
increaserow(mas, n, &m); // problem is here
for (int j = 0; j < n; j++){
mas[m-1][j] = 0;
}
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
printf("%d ", mas[i][j]);
}
printf("\n");
}
system("pause");
return 0;
}
I use this answer Resizing 2D Arrays in C like an example, something wrong.
The GNU Project Debugger on Windows:
warning: FTH: (9152): * Fault tolerant heap shim applied to current process. This is usually due to previous crashes. *
0 0 0
0 0 0
Program received signal SIGSEGV, Segmentation fault.
0x0000000000401821 in main (argc=1, argv=0x7f1990) at D:\III Курс! II СЕМЕСТР\МатМодДослОп\stud\Untitled2.c:47
47: mas[m-1][j] = 0;
Suppose in Gauss Elimination Method the first value of matrix, A[0][0]=0
Then how can I swap row 1 of matrix "A" with row 2, so that i get the correct result ?
Assuming a simpel 2d-array build the way as shown below: just exchange the rows.
#include <stdio.h>
#include <stdlib.h>
#define S_MATRIX_DIM 5
int main()
{
int **A;
int *tmp;
int i, j, entry;
entry = 0;
A = malloc(S_MATRIX_DIM * sizeof(int *));
for (i = 0; i < S_MATRIX_DIM; i++) {
A[i] = malloc(S_MATRIX_DIM * sizeof(int));
for (j = 0; j < S_MATRIX_DIM; j++) {
A[i][j] = entry++;
}
}
puts("Matrix A =");
for (i = 0; i < S_MATRIX_DIM; i++) {
for (j = 0; j < S_MATRIX_DIM; j++) {
printf("%d,", A[i][j]);
}
putchar('\n');
}
tmp = A[0];
A[0] = A[1];
A[1] = tmp;
puts("Matrix A, row exchanged =");
for (i = 0; i < S_MATRIX_DIM; i++) {
for (j = 0; j < S_MATRIX_DIM; j++) {
printf("%d,", A[i][j]);
}
putchar('\n');
}
for (i = 0; i < S_MATRIX_DIM; i++) {
free(A[i]);
}
free(A);
exit(EXIT_SUCCESS);
}
keep a temp value from the same type;
swap value by value,
example:
temp=a[i][j];
a[i][j]=a[i+1][j];
a[i+1][j]=temp;
I've presented the codes below only while executing the first loop it works fine but as soon as i uncomment the second loop it starts to throw segmentation fault. My code is as below.
// Write a program to add two m*n matrices using pointer.
#include <stdio.h>
#define m 2
#define n 2
int main() {
int (*a)[n];
int (*b)[n], i, j; //, *(sum)[n], i, j;
printf("Enter first matrix:\n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
scanf("%d", *(a + i) + j);
}
}
printf("Enter second matrix:\n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
scanf("%d", *(b + i) + j);
}
}
// printf("The Sum of matrix is:\n");
// for (i = 0; i < m; i++) {
// for (j = 0; j < n; j++) {
// // *(*(sum + i) + j) = *(*(a + i) + j) + *(*(b + i) + j);
// // printf("\t%d", *(*(sum + i) + j));
// }
// printf("\n");
// }
}
You are not defining a and b as 2D arrays, but as uninitialized pointers to 2D arrays. passing addresses into these invokes undefined behavior. You must make these pointers point to an actual array, either static, automatic or allocated from the heap.
You can define 2D arrays this way:
int a[m][n], b[m][n];
If you are required to use pointers, you can allocate the 2D arrays with malloc:
int (*a)[n] = malloc(sizeof(*a) * m);
int (*b)[n] = malloc(sizeof(*b) * m);
In your program, it is more readable to use the [] syntax, even for pointers:
#include <stdio.h>
#include <stdlib.h>
#define m 2
#define n 2
int main(void) {
int (*a)[n] = malloc(sizeof(*a) * m);
int (*b)[n] = malloc(sizeof(*b) * m);
int (*sum)[n] = malloc(sizeof(*sum) * m);
printf("Enter first matrix:\n");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &a[i][j]);
}
}
printf("Enter second matrix:\n");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &b[i][j]);
}
}
printf("The Sum of matrices is:\n");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
sum[i][j] = a[i][j] + b[i][j];
printf("\t%d", sum[i][j]);
}
printf("\n");
}
return 0;
}