« first day (1798 days earlier)      last day (1846 days later) » 

1:18 PM
Someone there, who can answer a question about the fork() systemcall?
 
user5291058
I am struggling with the following program: "Write a C program (using dynamic memory allocation and function) that can compute the addition of two matrices, report if not possible."
 
user5291058
I already have written a program using dynamic memory allocation but not function . Can anyone tell me how can I modify it?
 
user5291058
My program is given below:
 
user5291058
#include<stdio.h>

int main()
{
    int r1, c1, r2, c2, **a, **b, **c, i ,j;

    printf("Enter the number of rows of the first matrix:\n");
    scanf("%d",&r1);

    printf("Enter the number of columns of the matrix:\n");
    scanf("%d",&c1);

    printf("Enter the number of rows of the second matrix:\n");
    scanf("%d",&r2);

    printf("Enter the number of columns of the second matrix:\n");
    scanf("%d",&c2);

    if(c1!=c2 || r1!=r2)
    {
        printf("Addition is not possible.");
    }
 
very friendly.. @Stargateur
Well I'll ask anyway.
According to this answer: https://stackoverflow.com/questions/2562602/how-does-fork-return-for-child-process/23819945#23819945
fork() returns twice. Does that mean, that I have to call fork() twice, if I want to fork a child process? Because it would return 0 when called the first time and not creating a new child process?
 
1:41 PM
> Return Value
On success, the PID of the child process is returned in the parent, and 0 is returned in the child. On failure, -1 is returned in the parent, no child process is created, and errno is set appropriately.
 
2:27 PM
#include <stdio.h>
#include <stdlib.h>

static void free_matrix(int **matrix, size_t x) {
  for (size_t i = 0; i < x; i++) {
    free(matrix[i]);
  }
  free(matrix);
}

static int **new_matrix(size_t x, size_t y) {
  int **matrix = malloc(x * sizeof *matrix);
  if (matrix == NULL) {
    return NULL;
  }

  for (size_t i = 0; i < x; i++) {
    matrix[i] = malloc(y * sizeof **matrix);
    if (matrix[i] == NULL) {
      free_matrix(matrix, i);
      return NULL;
    }
  }

  return matrix;
}

static int fill_matrix(int **matrix, size_t x, size_t y) {
@user170039
 
user5291058
2:46 PM
@Stargateur: It is not compliling.
 
user5291058
It shows the following error:
 
user5291058
'for' loop initial declarations are only allowed in C99 or C11 mode
 
@user170039 Compile with --std=c11
 
user5291058
@dbush What's that?
 
@user170039 Assuming you're using gcc, an option to enable C11 mode
 
user5291058
2:50 PM
I am sorry, but I am very much new to programming and I don't understand what "using gcc" means. I am using this compiler.
 
@user170039 this is not compiler. This is just IDE: and editor with bells and whistles, which under the hood uses some other executable for a compiler, linker, etc.
 

« first day (1798 days earlier)      last day (1846 days later) »