PDA

View Full Version : Noob question about dynamic arrays

VanillaCone
November 5th, 2010, 06:32 AM
I'm doing an assignment for school and I've been brought to a standstill, I'm VERY new to programming, this is my first year so it's pretty difficult for me.

I'm getting a segmentation fault when i try to run this and im not sure why.

#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
int **createMatrix (int m, int n)
{
int **matrix;
int i;

matrix = (int**) malloc(m* sizeof (int*));
for (i=0; i<m; i++)
{
matrix[i]=(int*) malloc(n * sizeof (int));
}

return matrix;

}

int **readMatrix (int m, int n)
{
int **matrix = createMatrix(m, n);

int value;
int i = 0;

while (i < m)
{
int j = 0;
while (j < n)
{
scanf("%d", &value);
matrix[i][j] = value;
j++;
}
char newline;
scanf("%c", &newline);
i++;

}

}

void printMatrix (int **matrix, int m, int n)
{

int i;
int j;

for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("%10d", matrix[i][j]);
}

printf("\n");
}

}

int main (void)
{
int n = 4;
int m = 4;
int **A;

printf("Enter a 4x4 matrix:\n");

printMatrix(A, m, n);

return 0;

}

edit- also im trying to figure out how to post this code in a more readable fashion, i apologize ><

Kay I found the PHP button sorry about that
So the objective Of this assignment is to dynamically Allocate Memory for a matrix Of unknown Size, then read a user inputted matrix Into each individual Slot of the matrix, then print it out.

Both My Create matrix and read matrix functions work fine, it was after i implemented my printMatrix function That i got a segmentation fault, any advice would be appreciated I dont know how to continue :)

randumnumber
November 5th, 2010, 06:43 AM
for me i am able to read in an array 1 line at a time but after the 16th number it gives seg fault. so your reading in correctly...look through the part where you are printing the array.

this program should readlike this right?
enter 4x4 array
1
2
3
4
1
2
3
4
1
2
3
4
1
2
3
4

then print the array back right?

VanillaCone
November 5th, 2010, 06:52 AM
Ive been Inputting numbers like this:
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4

but i think itl Work both ways, and yes I know theres a problem with how im printing It im just not sure what it is, or what to change

randumnumber
November 5th, 2010, 07:04 AM

here are some examples of how to print an array in the fashion you are trying... try testing your method by hard coding an array and printing it. once that works try inputing the array.

VanillaCone
November 5th, 2010, 07:06 AM
Ya i found that site too, i tried their exact example but no luck.

randumnumber
November 5th, 2010, 07:07 AM
googling "print a matrix in c" bring up a ton of sites that could help. i took c and c++ but that was 4 years ago. and i dont remember much now.

randumnumber
November 5th, 2010, 07:08 AM
Does it have to use your subroutine structure?

randumnumber
November 5th, 2010, 07:14 AM
sorry i cant help more.

BUMP.

VanillaCone
November 5th, 2010, 07:22 AM
Its okay thanks for your help :), ill keep trying

worksofcraft
November 5th, 2010, 07:42 AM
well IMO you need to return a value from readMatrix.
Would you like me post a working version or you ok with that?

Some Penguin
November 5th, 2010, 08:13 AM
I would be very surprised if -Wall didn't print warnings for functions that declare a non-void return type but have paths which don't actually have return statements.

VanillaCone
November 5th, 2010, 08:39 AM
What Would i return From read matrix, Not sure what to write

VanillaCone
November 5th, 2010, 08:40 AM
NVM im so silly ^.^, I just put Return matrix, thank you very Much it works now!

GeneralZod
November 5th, 2010, 08:52 AM
Follow Some Penguin's advice and compile with GCC's -Wall and -Wextra options from now on: it will save you plenty of heartache in the future :)

NovaAesa
November 5th, 2010, 09:02 AM
Try the -pedantic option as well, I find it helps make sure code is written in a standard way.