Memory allocation for pointer to array and array of pointer - c

Below is the part of code I am stuck on. I want to dynamically allocate memory for
Pointer to array
Array of pointers
I am getting several error messages like invalid conversion from int * to int and so on.
Pointer to array
int (*array)[nrows][ncolumns];
array = (int*)malloc(nrows * ncolumns * sizeof(int));
printf("\n Enter the elements:\n");
for(i=0; i<nrows; i++)
{
for(j=0; j<ncolumns; j++)
{
scanf("%d", array[i][j]);
}
}
printf("Entered array is :\n\n");
for(i = 0;i<nrows; i++)
{
for(j = 0; j<ncolumns; j++)
{
if(j== ncolumns-1)
{
printf("%d \n", *array[i][j]);
}
else
{
printf("%d", *array[i][j]);
}
Array of pointers
int *array[nrows][ncolumns];
array[nrows][ncolumns] = (int*)malloc(nrows * ncolumns * sizeof(int));
printf("Enter elements:\n");
for(i = 0; i<nrows; i++)
{
for(j = 0; j<ncolumns;j++)
{
scanf("%d",&array[i][j]);
}
}
printf("Entered array is: \n");
for(i = 0; i<nrows; i++)
{
for(j = 0; j<ncolumns;j++)
{
if(j == ncolumns-1)
{
printf("%d \n",array[i][j]);
}
else
{
printf("%d \t",array[i][j]);
}
}
}

1> pointer to array
#include <stdio.h>
#include <stdlib.h>
int main(void){
int nrows = 3;
int ncolumns = 4;
int i, j;
int (*array)[nrows][ncolumns];//do you want <<int (*array)[ncolumns]>> ?
//like as int src[nrows][ncolumns]; array = &src;
array = malloc(nrows * ncolumns * sizeof(int));//(int*) : type mismatch
printf("\nEnter the elements:\n");
for(i = 0; i<nrows; i++){
for(j = 0; j<ncolumns; j++){
scanf("%d", &(*array)[i][j]);
}
}
printf("Entered array is :\n\n");
for(i = 0; i<nrows; i++){
for(j = 0; j<ncolumns; j++){
if(j != 0)
putchar(' ');
printf("%d", (*array)[i][j]);//need ( )
}
putchar('\n');
}
free(array);
return 0;
}
2> Array of pointers
#include <stdio.h>
#include <stdlib.h>
int main(void){
int nrows = 3;
int ncolumns = 4;
int i, j;
int *array[nrows][ncolumns];
int *src = (int*)malloc(nrows * ncolumns * sizeof(int));//no need (int*)
printf("Enter elements:\n");
for(i = 0; i<nrows; i++){
for(j = 0; j<ncolumns;j++){
array[i][j] = &src[ i * ncolumns + j];//pointer pointed to entity (src[ i * ncolumns + j])
scanf("%d", array[i][j]);//type of array[i][j] is int *
}
}
printf("Entered array is: \n");
for(i = 0; i<nrows; i++){
for(j = 0; j<ncolumns; j++){
if(j != 0)
putchar(' ');
printf("%d", *array[i][j]);//need * for dereference
}
putchar('\n');
}
free(src);
return 0;
}
3> option
#include <stdio.h>
#include <stdlib.h>
int main(void){
int nrows = 3;
int ncolumns = 4;
int i, j;
int (*array)[ncolumns];
array = (int (*)[ncolumns])malloc(nrows * sizeof(*array));//sizeof(*array) : sizeof(int[ncolumns])
printf("\nEnter the elements:\n");
for(i = 0; i<nrows; i++){
for(j = 0; j<ncolumns; j++){
scanf("%d", &array[i][j]);
}
}
printf("Entered array is :\n\n");
for(i = 0; i<nrows; i++){
for(j = 0; j<ncolumns; j++){
if(j != 0)
putchar(' ');
printf("%d", array[i][j]);
}
putchar('\n');
}
free(array);
return 0;
}

Related

Problem adding two 4x4 matrices and printing the sum result in a new matrix in C

Could someone help me please?
I need to perform the sum of two matrices that the data will be sent by the user and print the result in a new matrix.
I managed to capture the data from the two arrays, but when I try to add the two, the code does not print the sum, where is the error?
Thanks
#include <stdio.h>
#include <stdlib.h>
void sum(int *mat_A, int *mat_B, int *mat_C);
int main() {
int mat_A[4][4];
int mat_B[4][4];
int mat_C[4][4];
int i, j, value;
printf("\nEnter integer values ​​for the elements of matrix A: \n");
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
printf("\nElement[%d][%d] = ", i, j);
scanf_s("%d", &value);
mat_A[i][j] = value;
}
}
printf("\nEnter integer values ​​for the elements of matrix B: \n");
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
printf("\nElement[%d][%d] = ", i, j);
scanf_s("%d", &value);
mat_B[i][j] = value;
}
}
calc_soma(*mat_A, *mat_B, *mat_C);
printf("\nSum of matrices A with B: \n\n");
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
value = mat_C[i][j];
printf("%d", value);
}
printf("\n");
}
return 0;
}
void sum(int *mat_A, int *mat_B, int *mat_C) {
int i, j;
int value;
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
value = *mat_A + *mat_B;
*mat_C = value;
}
}
}
You need to declare the parameters to sum() as 2-dimensional arrays, not int *, which is a pointer to a 1-dimensional array. Then use i and j as array indexes.
You're also calling the function with the wrong name calc_soma.
#include <stdio.h>
#include <stdlib.h>
void sum(int mat_A[4][4], int mat_B[4][4], int mat_C[4][4]);
int main() {
int mat_A[4][4];
int mat_B[4][4];
int mat_C[4][4];
int i, j, value;
printf("\nEnter integer values for the elements of matrix A: \n");
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
printf("\nElement[%d][%d] = ", i, j);
scanf_s("%d", &value);
mat_A[i][j] = value;
}
}
printf("\nEnter integer values for the elements of matrix B: \n");
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
printf("\nElement[%d][%d] = ", i, j);
scanf_s("%d", &value);
mat_B[i][j] = value;
}
}
sum(mat_A, mat_B, mat_C);
printf("\nSum of matrices A with B: \n\n");
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
value = mat_C[i][j];
printf("%d", value);
}
printf("\n");
}
return 0;
}
void sum(int mat_A[4][4], int mat_B[4][4], int mat_C[4][4]) {
int i, j;
int value;
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
value = mat_A[i][j] + mat_B[i][j];
mat_C[i][j] = value;
}
}
}

Cannot input number in multi-dimensional array pointer to pointer

#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.

Array[0] is changing while entering the "for" loop, can't figure out why

I'm doing a challenge on HackerRank got the method figured out but there's a slight error I cannot figure out. Further information if needed is https://www.hackerrank.com/challenges/sparse-arrays
Basically I only have a problem with arr[0]. It's storing arr[0] as 'aba', then once it hits the first for loop it changes to 'ab'. Why?
Input:
4
aba
baba
aba
xzxb
3
aba
xzxb
ab
Code:
int main() {
int i, j;
int n;
int q;
scanf("%d", &n);
char* arr[n];
char* test[q];
char* s;
int counter[q];
for (i = 0; i < q; i++) {
counter[i] = 0;
}
for (i = 0; i < n; i++) {
arr[i] = malloc(20);
scanf("%s", arr[i]);
}
scanf("%d", &q);
for (i = 0; i < q; i++) {
test[i] = malloc(20);
scanf("%s", test[i]);
}
for (i = 0; i < n; i++) {
for (j = 0; j < q; j++) {
if (strcmp(arr[i], test[j]) == 0) {
counter[j]++;
} else {
}
}
}
for (i = 0; i < q; i++) {
printf("%d\n", counter[i]);
}
return 0;
}
You declared test and counter as array of size q before having initialize q. Move there declaration just after scanf("%d",&q);. Also move the initializing loop of counter :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
int i, j;
int n;
int q;
scanf("%d", &n);
char* arr[n];
char* s;
for(i=0; i<n; i++) {
arr[i]= malloc(20);
scanf("%s",arr[i]);
}
scanf("%d", &q);
int counter[q];
char* test[q];
for(i=0; i<q; i++) {
counter[i] = 0;
}
for(i=0; i<q; i++) {
test[i]= malloc(20);
scanf("%s",test[i]);
}
for(i=0; i<n; i++) {
for(j=0; j<q; j++) {
if (strcmp(arr[i],test[j]) == 0) {
counter[j]++;
}
}
}
for(i=0; i<q; i++) {
printf("%d\n", counter[i]);
}
return 0;
}
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int i, j;
int n;
int q;
scanf("%d", &n);
char* arr[n];
for (i = 0; i < n; i++) {
arr[i] = malloc(20);
scanf("%s", arr[i]);
}
scanf("%d", &q);
char* test[q];
char* s;
int counter[q];
for (i = 0; i < q; i++) {
counter[i] = 0;
}
for (i = 0; i < q; i++) {
test[i] = malloc(20);
scanf("%s", test[i]);
}
for (i = 0; i < n; i++) {
for (j = 0; j < q; j++) {
if (strcmp(arr[i], test[j]) == 0) {
counter[j]++;
} else {
}
}
}
for (i = 0; i < q; i++) {
printf("%d\n", counter[i]);
}
return 0;
}
try this, use varialble after declaration and initialization

function not printing correctly in c

I am trying to build a dynamic maze i got to the part where i get the size and get the chars that i need to build the maze from them.
but the function thats build the maze prints it really asymmetrical how can i fix that?
my code:
#include <stdio.h>
#include <stdlib.h>
char **board;
int size = 0;
void print_Board();
void initialize_Board();
int main()
{
initialize_Board();
print_Board();
return 0;
}
/*initialize the board*/
void initialize_Board()
{
int i, j;//indexs
char s;
scanf("%d", &size);
board = (char**)malloc(sizeof(char*)* (size));
if (!board) { printf("ERROR - memroy allocation.\n"); exit(1); }
for (i = 0; i < size; i++)//for loops to build the board for the game
{
board[i] = (char*)malloc(sizeof(char)*(size));
if (!board[i]) { printf("ERROR - memroy allocation, for loop\n");
exit(1);
}
for (j = 0; j < size; j++)
{
scanf("%c", &s);
board[i][j] = s;
}//for col
printf("\n");
}//for row
}
//print the board
void print_Board()
{
int i, j;
for (i = 0; i < size; i++)
{
for (j = 0; j < size; j++)
{
printf("%c ", board[i][j]); //print the value in the [i][j] place.
}//for col
printf("\n");
}//for row
}
Change:
for (j = 0; j < size; j++)
{
scanf("%c", &s);
board[i][j] = s;
}//for col
To:
for (j = 0; j < size; j++) {
scanf("%c ", &s);
board[i][j] = s;
}//for col
board[i][j] = '\n'; // Add new line to end of row making it a string.
This ensures that each character is read and the return char is discarded.
and change:
int i, j;
for (i = 0; i < size; i++)
{
for (j = 0; j < size; j++)
{
printf("%c ", board[i][j]); //print the value in the [i][j] place.
}//for col
printf("\n");
}//for row
to:
int i;
for (i = 0; i < size; i++) {
printf("%s", board[i]); //print the values in the [i] row.
}
This prints each row with a newline at the end.

Unhandled exception in c

my code is compiled well, and executed. But I get this error after I enter the sortType value: "Unhandled exception at 0x52a56af2 (msvcr90d.dll) in ALINUR_CAGLAYAN_LAB6.exe: 0xC0000005: Access violation writing location 0x00000000."
Here's my code:
#include <stdio.h>
int sorting(int *liverpool8, int *besiktas0, int *hahaha);
void main()
{
int *numbers;
int length;
int sortType=0;
int i;
printf("Enter the length of array:");
scanf("%d",&length);
numbers = (int*)malloc(length*sizeof(int));
for (i = 0; i < length; ++i)
{
printf("%d. element: ", i+1);
scanf("%d", &numbers[i]);
}
printf("\nPlease select one of the following functions:\n1)Ascending order\n0)Descending order");
scanf("%d", sortType);
sorting(*numbers, &length, &sortType);
printf("The numbers arranged in the order as you entered are given below\n");
for (i = 0; i < length; ++i)
{
printf("%d\n", numbers[i]);
}
system("pause");
}
int sorting(int *numbers, int *length, int *sortType)
{
int j, i, a;
if(sortType == 1)
{
for (i = 0; i < length; ++i)
{
for (j = i + 1; j < length; ++j)
{
if (numbers[i] > numbers[j])
{
a = numbers[i];
numbers[i] = numbers[j];
numbers[j] = a;
}
}
}
}
else if(sortType == 0)
{
for (i = 0; i < length; ++i)
{
for (j = i + 1; j < length; ++j)
{
if (numbers[i] < numbers[j])
{
a = numbers[i];
numbers[i] = numbers[j];
numbers[j] = a;
}
}
}
return *numbers;
}
}
There are multiple errors in your code.
The header file for using malloc is not defines.
scanf needs to access addresses and not variables.
The array name decays to a pointer so using * for an array is double dereferencing
To access value in pointer you have to use *, the dereferencing pointer.
I have attached the corrected code.
#include <stdio.h>
#include <stdlib.h>
int sorting(int *liverpool8, int *besiktas0, int *hahaha);
void main()
{
int *numbers;
int length;
int sortType=0;
int i;
printf("Enter the length of array:");
scanf("%d",&length);
numbers = (int*)malloc(length*sizeof(int));
for (i = 0; i < length; ++i)
{
printf("%d. element: ", i+1);
scanf("%d", &numbers[i]);
}
printf("\nPlease select one of the following functions:\n1)Ascending order\n0)Descending order");
scanf("%d", &sortType);
sorting(numbers, &length, &sortType);
printf("The numbers arranged in the order as you entered are given below\n");
for (i = 0; i < length; ++i)
{
printf("%d\n", numbers[i]);
}
system("pause");
}
int sorting(int *numbers, int *length, int *sortType)
{
int j, i, a;
if(*sortType == 1)
{
for (i = 0; i < *length; ++i)
{
for (j = i + 1; j < *length; ++j)
{
if (numbers[i] > numbers[j])
{
a = numbers[i];
numbers[i] = numbers[j];
numbers[j] = a;
}
}
}
}
else if(sortType == 0)
{
for (i = 0; i < *length; ++i)
{
for (j = i + 1; j < *length; ++j)
{
if (numbers[i] < numbers[j])
{
a = numbers[i];
numbers[i] = numbers[j];
numbers[j] = a;
}
}
}
return *numbers;
}}

Resources