passing 2d array to function using pointers in c - c

this is first time I've had to ask a question here usually don't need but this time i just can't figure it out.
Basically i'm trying to create a grid in the console of 1s and 0s (1s and 0s in random places). the 1s being wood chips and 0s representing blank space.
to do this i have to use this function which initializes and populates a 2d array and returns the pointer to the finished array
int* createEnvironment(int width, int height, int numWoodChips)
i then then need to print the 2d array using this function
void printEvrionment(int* environment, int width, int height)
this is what i have so far
int enviroWidth = 20;
int enviroHeight = 20;
int* createEnvironment(int width, int height, int numWoodChips);
void printEvrionment(int* environment, int width, int height);
void freeArray(int width, int* environment);
int main(int argc, char **argv)
{
int *arr = createEnvironment(enviroWidth, enviroHeight, 10);
printEvrionment(arr, enviroWidth, enviroHeight);
getchar();
freeArray(enviroHeight, arr);
return 0;
}
int* createEnvironment(int width, int height, int numWoodChips)
{
int Num = 1;
/*randNum = (rand() % 1) + 0;*/
int temp = 3;
int woodChipCounter = 0;
int counter = 0;
int *array;
array = malloc(sizeof(array)*height *width);
for (int j = 0; j < height; j++)
{
for (int i = 0; i < width; i++)
{
counter++;
if (counter == temp)
{
array[j*width + i] = 1;
temp++;
woodChipCounter++;
counter = 0;
}
else
{
array[j*width + i] = 0;
}
if (woodChipCounter == numWoodChips)
{
Num = 0;
}
}
}
return array;
}
void printEvrionment(int* environment, int width, int height)
{
for (int j = 0; j < height; j++)
{
printf("\n");
for (int i = 0; i < width; i++)
{
printf("%d", environment[j]);
}
}
}
void freeArray(int height, int* environment)
{
for (int i = 0; i < height; i++)
{
free(environment[i]);
}
free(environment);
}

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
int enviroWidth = 20;
int enviroHeight = 20;
char *createEnvironment(int width, int height, int numWoodChips);
void printEvrionment(char *environment, int width, int height);
//void freeArray(int width, int* environment);
int main(int argc, char **argv){
char *arr = createEnvironment(enviroWidth, enviroHeight, 10);
printEvrionment(arr, enviroWidth, enviroHeight);
getchar();
//freeArray(enviroHeight, arr);
free(arr);
return 0;
}
char *createEnvironment(int width, int height, int numWoodChips){
srand(time(NULL));
int len = width * height;
char *array = malloc(len);
if(numWoodChips > len)
numWoodChips = len;
memset(array, '1', numWoodChips);
memset(array + numWoodChips, '0', len - numWoodChips);
for(int i=0;i<numWoodChips; ++i){
char tmp;
int pos = rand()%len;
//swap array[i] and array[pos]
tmp = array[i];
array[i] = array[pos];
array[pos] = tmp;
}
return array;
}
void printEvrionment(char *environment, int width, int height){
printf("\n");
for (int j = 0; j < height; ++j){
for (int i = 0; i < width; ++i){
printf("%c", environment[width * j + i]);
}
printf("\n");
}
}

Related

I'm try to allocate um String matrix with malloc, but returns segmantation fault, whats happen?

I'm try to allocate an string matrix, but, in the last line mt code returns Segmentation Fault, how can i fix it?
char **allocate(char ***map, int lin, int col){
int index = lin;
map = (char*** ) malloc(sizeof(char) * lin);
for(int i = 0; i < index; i++){
map[i] = (char**) malloc(sizeof(char) * col);
}
return (char**) map;
}
void **fill(char ***map, int index){
printf("index: %d\n", index);
for(int i = 0; i <index; ++i){
for (int j = 0; j < index; ++j){
map[i][j] = "aaaaaaaaa";
printf("%s ", map[i][j]);
}
printf("\n");
}
}
int main(){
char **map = NULL;
map = allocate(map,5,5);
printf("\n");
fill(map,5);
return 0;
}
I expect only to show the last line of my matrix.
I guess you mean something like the following.
Note the "const" as a string is a "const char *" not a "char *".
#include <malloc.h>
#include <stdio.h>
const char ***allocate(int lin, int col){
int index = lin;
const char ***map = (const char*** ) malloc(sizeof(char**) * lin);
for(int i = 0; i < index; i++){
map[i] = (const char**) malloc(sizeof(char*) * col);
}
return map;
}
void **fill(const char ***map, int index){
printf("index: %d\n", index);
for(int i = 0; i < index; ++i){
for (int j = 0; j < index; ++j){
map[i][j] = "aaaaaaaaa";
printf("%s ", map[i][j]);
}
printf("\n");
}
}
int main(){
const char ***map = allocate(5,5);
printf("\n");
fill(map,5);
return 0;
}
Calling malloc this many times and having such a high level of indirection
makes for very inefficient coding.
It is sufficient in this case to make a constant 5x5 array of const char *
int main() {
const char *map[5][5];
for (int i = 0; i != 5; ++i) {
for (int j = 0; j != 5; ++j) {
map[i][j] = "I should write my own assignment.";
}
}
}

Why does my addArrays function get a wedge exit compile code? (UPDATE FIXED)

The rest of my functions work fabulously, however the last function has my goat. The goal of this function is to use pointers to obtain the values of two different arrays and add those values to a third array. However, when I run the main method to make the function run, it pauses for a second and provides a wedge exit code that does not work.
I've tried removing the if((sizeof(*ptr1)) == (sizeof(*ptr2)){
---insert code here---
}
from the for loop, however, the problem seems to be just the for loop itself.
//===================================Broken Code========================================
#include <stdio.h>
#define MAXIMUM 1000
int sumArrays(int arr1[], int arr2[]);
int addArrays(int arr1[], int arr2[]);
int main()
{
int arrayOne[MAXIMUM];
int arrayTwo[MAXIMUM];
for(int i = 0; i <= MAXIMUM; i++)
arrayOne[i] = i;
printf("Arrayone %d\n", arrayOne);
for(int j = 0; j <= MAXIMUM; j++)
arrayTwo[j] = j;
printf("ArrayTwo %d\n", arrayTwo);
printf(" The sum of the arrays is : %d\n",sumArrays(arrayOne, arrayTwo));
printf("%d", addArrays(arrayOne, arrayTwo));
return 0;
}
int sumArrays(int arr1[],int arr2[]){
int *ptr_1;
int *ptr_2;
ptr_1 = &arr1[0];
ptr_2 = &arr2[0];
int sum;
for(int i = 0; i < MAXIMUM; i++){
sum += *ptr_1 + i;
sum += *ptr_2 + i;
}
return sum;
}
int addArrays(int arr1[],int arr2[]){
int *ptr1 = &arr1[0];
int *ptr2 = &arr2[0];
int sum = 0;
int i = 0;
int arr3[0];
if(sizeof(*ptr1) == sizeof(*ptr2)){
for(int i = 0; i < MAXIMUM; i++){
sum += *ptr1 +i;
sum += *ptr2 +i;
arr3[i] = sum;
}
}
printf("The value of array3 is %d", arr3);
}
The other function works perfectly, but the addArrays function does a wedge exit and doesn't cooperate.
I expect the addArrays function to take the elements from each array, add them together and assign them to the third array.
Thank you for your time.
UPDATE: WORKING CODE
#include <stdio.h>
#define MAXIMUM 1000
#define ARRAY_SZ(x) (sizeof(x) / sizeof((x)[0]))
int sumArrays(int arr1[], int arr2[], size_t len);
int addArrays(int arr1[], int arr2[], int arr3[], size_t len);
int main()
{
int arrayOne[MAXIMUM];
int arrayTwo[MAXIMUM];
int arrayThree[MAXIMUM];
for(int i = 0; i <= MAXIMUM; i++)
arrayOne[i] = i;
printf("Array One %d\n", ARRAY_SZ(arrayOne));
for(int j = 0; j <= MAXIMUM; j++)
arrayTwo[j] = j;
printf("Array Two %d\n", ARRAY_SZ(arrayTwo));
printf(" The sum of the arrays is : %d\n",sumArrays(arrayOne, arrayTwo, ARRAY_SZ(arrayOne)));
printf("%d", addArrays(arrayOne, arrayTwo, arrayThree, MAXIMUM));
return 0;
}
int sumArrays(int arr1[],int arr2[], size_t len){
int *ptr_1;
int *ptr_2;
ptr_1 = &arr1[0];
ptr_2 = &arr2[0];
int sum = 0 ;
for(int i = 0; i < len; i++){
sum += *ptr_1++;
sum += *ptr_2++;
}
return sum;
}
int addArrays(int arr1[],int arr2[], int result[], size_t len){
int *ptr1 = &arr1[0];
int *ptr2 = &arr2[0];
int *ptr3 = &result[0];
int sum = 0;
int sum2 = 0;
int i = 0;
for(int i = 0; i < MAXIMUM; i++){
sum = *ptr1 ++;
sum += *ptr2 ++;
result[i] = sum;
printf("The result of array 3 is %d\n", *ptr3++);
}
}
Here are some notes:
When you assign/pass/print the and array using the name of the array, you are actually passing the memory location of the first element in the array (a pointer).So when you write:
printf("Arrayone %d\n", arrayOne);
You will see the memory address of the first element of the array being printed. If you would like to print the entire array you will need to loop through it. In this case you would be printing 1000 integers which might be undesirable.
void printArray(int * array, size_t len)
{
while(len--)
{
printf("%d ", *array++);
}
}
To get the number of elements in an array you can do something like this:
sizeof(arrayOne) / sizeof(arrayOne[0])
and you can put it in a macro like this:
#define ARRAY_SZ(x) (sizeof(x) / sizeof((x)[0]))
and call it like this:
ARRAY_SZ(arrayOne);
You cannot get the array size if you are receiving an array in a function (it has decayed to a pointer), instead you should pass the array size to the function too. Here because you initialize the arrays with the size MAXIMUM we don't actually need to calculate the array size, but we can just to show it works.
If you want to return an array (like in addArrays()) you should create an empty array and pass it to the function, then the function can update the array with the result.
When looping through an array you never want to do array[maximum] because the array indices range from 0 to maximum - 1
#include <stdio.h>
#define MAXIMUM 1000
#define ARRAY_SZ(x) (sizeof(x) / sizeof((x)[0]))
int sumArrays(int arr1[], int arr2[]);
int addArrays(int arr1[], int arr2[]);
int main()
{
int arrayOne[MAXIMUM];
int arrayTwo[MAXIMUM];
int arrayThree[MAXIMUM];
for(int i = 0; i < MAXIMUM; i++)
arrayOne[i] = i;
printf("Array one size %d\n", ARRAY_SZ(arrayOne));
for(int j = 0; j < MAXIMUM; j++)
arrayTwo[j] = j;
printf("Array Two size %d\n", ARRAY_SZ(arrayTwo));
printf(" The sum of the arrays is : %d\n",sumArrays(arrayOne, arrayTwo, ARRAY_SZ(arrayOne)));
addArrays(arrayOne, arrayTwo, arrayThree, MAXIMUM);
return 0;
}
int sumArrays(int arr1[],int arr2[], size_t len)
{
int *ptr_1;
int *ptr_2;
ptr_1 = &arr1[0];
ptr_2 = &arr2[0];
int sum;
for(int i = 0; i < len; i++){
sum += *ptr_1 + i;
sum += *ptr_2 + i;
}
return sum;
}
void addArrays(int arr1[], int arr2[], int result[], size_t len){
int *ptr1 = arr1;
int *ptr2 = arr2;
int sum = 0;
int i = 0;
for(int i = 0; i < len; i++){
sum = *ptr1 +i;
sum += *ptr2 +i;
result[i] = sum;
}
}

Function pointer in argument in C

I have to do an exercise where I have a certain numbers of functions and every function do a different thing like sort all the negative numbers from the array.
Moreover I have to create a function display with 3 argument pointers to an array, size of it and a name of a function which receives int and that the issue is int (Function pointer). I try to do this but this don't work and I don't know what to do in order to do correctly this exercise with a function pointer, because I don't understand that.
This is my code
int main (int argc, char **argv)
{
srand (time (NULL));
int arr[MAX_SIZE], second_arr[MAX_SIZE], i;
random_arr (arr);
display (arr, 20, negative_number (arr, second_arr));
system ("PAUSE");
return 0;
}
void random_arr (int *my_arr)
{
int i;
for (i = 0; i < MAX_SIZE; i++) {
*(my_arr + i) = i - 10;
}
}
int negative_number (int *arr, int *sort_arr)
{
int i;
for (i = 0; i < 20; i++) {
if (arr[i] < 0) {
sort_arr[i] = arr[i];
}
}
return sort_arr;
}
void diplay (int *arr, int size, int (*a_function) (int, int))
{
int i = 0;
for (i = 0; i < size; i++) {
printf ("%d\n", a_function);
}
}
It might be different from your intentions because your intentions is not clear.
but I think this would be helpful
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX_SIZE 20
void random_arr (int size, int *my_arr);
int negative_number (int size, int *arr, int *sort_arr);
void display (int *arr, int size, int (*filter_function) (int in_size, int *in_array, int *out_array));
int main (void){
int arr[MAX_SIZE], second_arr[MAX_SIZE], i;
srand(time(NULL));
random_arr(MAX_SIZE, arr);
for(i = 0; i < MAX_SIZE; ++i)
printf("%d ", arr[i]);
puts("");
display (arr, MAX_SIZE, negative_number);
system ("PAUSE");
return 0;
}
void random_arr (int size, int *my_arr){
int i;
for (i = 0; i < size; i++) {
my_arr[i] = rand()%MAX_SIZE - MAX_SIZE/2;
}
}
int negative_number (int size, int *arr, int *sort_arr){
int i, j = 0;
for (i = 0; i < size; i++) {
if (arr[i] < 0) {
sort_arr[j++] = arr[i];
}
}
return j;//new array size
}
void display (int *arr, int size, int (*filter)(int in_size, int *in_array, int *out_array)){
int i = 0;
int *out = malloc(size * sizeof(*out));
int out_size = filter(size, arr, out);
for (i = 0; i < out_size; i++) {
printf ("%d\n", out[i]);
}
free(out);
}

c- Allocate and Free 2D Array

I am trying to write 2 functions, one to Dynamic allocation a 2D array,Other to free this 2D array:
int allocate(int **array, unsigned int rows, unsigned int columns){
int i;
for (i = 0; i < rows; i++) {
array[i] = malloc(columns * sizeof (int));
}
/* Code fo fill the array*/
return 1;
}
void de_allocate(int **v, unsigned int rows) {
int i;
for (i = 0; i < rows; i++) {
free(v[i]);
}
free(v);
}
int main(int argc, char **argv) {
int rows, columns;
rows = atoi(argv[1]);
columns = atoi(argv[2]);
int *ipp[rows];
allocate(ipp, rows, columns);
de_allocate(ipp,rows);
return 0;
}
I must respect the allocate function signature :
int allocate(int **array, unsigned int rows, unsigned int columns)
And at the end of allocate function ipp must have access to allocated 2D array.
Allocate function it's right but in de_allocate function i have a SIGABRT Signal
The problem is that you are trying to free a stack allocated var with code free(v);
You could do that if you mallocated the array of pointer, but you declare it locally in main function with int *ipp[rows];
Change it to int **ipp = malloc(sizeof(int*)*rows); if you want to leave de_allocate as is.
You could test it with
#include <stdio.h>
#include <stdlib.h>
int allocate(int **array, unsigned int rows, unsigned int columns){
int i;
for (i = 0; i < rows; i++)
{
array[i] = malloc(columns * sizeof (int));
}
/* Code fo fill the array*/
return 1;
}
void de_allocate(int **v, unsigned int rows) {
int i;
for (i = 0; i < rows; i++)
{
free(v[i]);
}
free(v);
}
int main(int argc, char **argv)
{
int rows, columns;
int temp = 0;
rows = atoi(argv[1]);
columns = atoi(argv[2]);
int **ipp = malloc(sizeof(int*)*rows);
allocate(ipp, rows, columns);
for (int i=0; i<rows; i++)
for (int j=0; j<columns; j++)
ipp[i][j] = temp++;
for (int i=0; i<rows; i++)
for (int j=0; j<columns; j++)
printf("ipp[%d][%d] = %d\n", i, j, ipp[i][j]);
de_allocate(ipp,rows);
return 0;
}
void de_allocate(int **v, unsigned int rows) {
int i;
for (i = 0; i < rows; i++) {
free(v[i]);
}
free(v);
-----^----
here you are attempting to free a variable for which you didn't dynamically allocate memory in the first place
}

crash on trying to reallocate a pointer using pointer to this pointer

I have a pointer to a pointer ("paths") and I want to reallocate each pointer (each "path"). But I get a crash. Generally I am trying to find all possible powers of a number, which one can compute for some amount of operations (e.g for two operations we can get power of three and four (one operation for square of a number, then another one either for power of three or four)). I figured out how to do it on paper, now I am trying to implement it in code. Here is my try:
#include <stdio.h>
#include <stdlib.h>
void print_path(const int *path, int path_length);
int main(void)
{
fputs("Enter number of operations? ", stdout);
int operations;
scanf("%i", &operations);
int **paths, *path, npaths, npath;
npaths = npath = 2;
path = (int*)malloc(npath * sizeof(int));
paths = (int**)malloc(npaths * sizeof(path));
int i;
for (i = 0; i < npaths; ++i) // paths initialization
{
int j;
for (j = 0; j < npath; ++j)
paths[i][j] = j+1;
}
for (i = 0; i < npaths; ++i) // prints the paths, all of them are displayed correctly
print_path(paths[i], npath);
for (i = 1; i < operations; ++i)
{
int j;
for (j = 0; j < npaths; ++j) // here I am trying to do it
{
puts("trying to reallocate");
int *ptemp = (int*)realloc(paths[j], (npath + 1) * sizeof(int));
puts("reallocated"); // tried to write paths[j] = (int*)realloc...
paths[j] = ptemp; // then tried to make it with temp pointer
}
puts("memory reallocated");
++npath;
npaths *= npath; // not sure about the end of the loop
paths = (int**)realloc(paths, npaths * sizeof(path));
for (j = 0; j < npaths; ++j)
paths[j][npath-1] = paths[j][npath-2] + paths[j][j];
for (j = 0; j < npaths; ++j)
print_path(paths[j], npath);
puts("\n");
}
int c;
puts("Enter e to continue");
while ((c = getchar()) != 'e');
return 0;
}
void print_path(const int *p, int pl)
{
int i;
for (i = 0; i < pl; ++i)
printf(" A^%i -> ", p[i]);
puts(" over");
}
I am not sure the problem resides with the call to realloc(), rather you are attempting to write to locations for which you have not created space...
Although you create memory for the pointers, no space is created (allocate memory) for the actual storage locations.
Here is an example of a function to allocate memory for a 2D array of int:
int ** Create2D(int **arr, int cols, int rows)
{
int space = cols*rows;
int y;
arr = calloc(space, sizeof(int));
for(y=0;y<cols;y++)
{
arr[y] = calloc(rows, sizeof(int));
}
return arr;
}
void free2DInt(int **arr, int cols)
{
int i;
for(i=0;i<cols; i++)
if(arr[i]) free(arr[i]);
free(arr);
}
Use example:
#include <ansi_c.h>
int main(void)
{
int **array=0, i, j;
array = Create2D(array, 5, 4);
for(i=0;i<5;i++)
for(j=0;j<4;j++)
array[i][j]=i*j; //example values for illustration
free2DInt(array, 5);
return 0;
}
Another point here is that it is rarely a good idea to cast the return of [m][c][re]alloc() functions
EDIT
This illustration shows my run of your code, just as you have presented it:
At the time of error, i==0 & j==0. The pointer at location paths[0][0] is uninitialized.
EDIT 2
To reallocate a 2 dimension array of int, you could use something like:
int ** Realloc2D(int **arr, int cols, int rows)
{
int space = cols*rows;
int y;
arr = realloc(arr, space*sizeof(int));
for(y=0;y<cols;y++)
{
arr[y] = calloc(rows, sizeof(int));
}
return arr;
}
And here is a test function demonstrating how it works:
#include <stdio.h>
#include <stdlib.h>
int ** Create2D(int **arr, int cols, int rows);
void free2DInt(int **arr, int cols);
int ** Realloc2D(int **arr, int cols, int rows);
int main(void)
{
int **paths = {0};
int i, j;
int col = 5;
int row = 8;
paths = Create2D(paths, col, row);
for(i=0;i<5;i++)
{
for(j=0;j<8;j++)
{
paths[i][j]=i*j;
}
}
j=0;
for(i=0;i<5;i++)
{
for(j=0;j<8;j++)
{
printf("%d ", paths[i][j]);
}
printf("\n");
}
//reallocation:
col = 20;
row = 25;
paths = Realloc2D(paths, col, row);
for(i=0;i<20;i++)
{
for(j=0;j<25;j++)
{
paths[i][j]=i*j;
}
}
j=0;
for(i=0;i<20;i++)
{
for(j=0;j<25;j++)
{
printf("%d ", paths[i][j]);
}
printf("\n");
}
free2DInt(paths, col);
getchar();
return 0;
}
The realloc() does not fail. What fails is that you haven't allocated memory for the new pointers between paths[previous_npaths] and paths[new_npaths-1], before writing to these arrays in the loop for (j = 0; j < npaths; ++j).

Resources