Scanning to 2d array and then passing it to function - C
My program is getting segmentation fault if I allocate function as 1D array and then pass it to function. It is built for 2d array. Problem is, that I can't find out how to allocate 2d array and how to pass it correctly into function. Hope all is explained clearly. If you know what is wrong please try to lead me on correct way to fix it. Many thanks. Here is code:
int main()
{
int i, j, size;
scanf("%d", &size);
int *a;
//here i try to allocate it as 2d array
*a = (int *)malloc(size * sizeof(int));
for (i=0; i<size; i++)
{
a[i] = (int *)malloc(size * sizeof(int));
}
//here i scan value to 2d array
for (i = 0; i < size; i++)
for (j = 0; j < size; j++){
scanf("%d", &a[i][j]); }
//here i pass array and size of it into function
if (is_magic(a,size))
function header looks like:
int is_magic(int **a, int n)
c arrays 2d
add a comment |
My program is getting segmentation fault if I allocate function as 1D array and then pass it to function. It is built for 2d array. Problem is, that I can't find out how to allocate 2d array and how to pass it correctly into function. Hope all is explained clearly. If you know what is wrong please try to lead me on correct way to fix it. Many thanks. Here is code:
int main()
{
int i, j, size;
scanf("%d", &size);
int *a;
//here i try to allocate it as 2d array
*a = (int *)malloc(size * sizeof(int));
for (i=0; i<size; i++)
{
a[i] = (int *)malloc(size * sizeof(int));
}
//here i scan value to 2d array
for (i = 0; i < size; i++)
for (j = 0; j < size; j++){
scanf("%d", &a[i][j]); }
//here i pass array and size of it into function
if (is_magic(a,size))
function header looks like:
int is_magic(int **a, int n)
c arrays 2d
Do not usex = (int *)malloc...
- usex = malloc..
– SergeyA
Dec 27 '18 at 18:42
add a comment |
My program is getting segmentation fault if I allocate function as 1D array and then pass it to function. It is built for 2d array. Problem is, that I can't find out how to allocate 2d array and how to pass it correctly into function. Hope all is explained clearly. If you know what is wrong please try to lead me on correct way to fix it. Many thanks. Here is code:
int main()
{
int i, j, size;
scanf("%d", &size);
int *a;
//here i try to allocate it as 2d array
*a = (int *)malloc(size * sizeof(int));
for (i=0; i<size; i++)
{
a[i] = (int *)malloc(size * sizeof(int));
}
//here i scan value to 2d array
for (i = 0; i < size; i++)
for (j = 0; j < size; j++){
scanf("%d", &a[i][j]); }
//here i pass array and size of it into function
if (is_magic(a,size))
function header looks like:
int is_magic(int **a, int n)
c arrays 2d
My program is getting segmentation fault if I allocate function as 1D array and then pass it to function. It is built for 2d array. Problem is, that I can't find out how to allocate 2d array and how to pass it correctly into function. Hope all is explained clearly. If you know what is wrong please try to lead me on correct way to fix it. Many thanks. Here is code:
int main()
{
int i, j, size;
scanf("%d", &size);
int *a;
//here i try to allocate it as 2d array
*a = (int *)malloc(size * sizeof(int));
for (i=0; i<size; i++)
{
a[i] = (int *)malloc(size * sizeof(int));
}
//here i scan value to 2d array
for (i = 0; i < size; i++)
for (j = 0; j < size; j++){
scanf("%d", &a[i][j]); }
//here i pass array and size of it into function
if (is_magic(a,size))
function header looks like:
int is_magic(int **a, int n)
c arrays 2d
c arrays 2d
asked Dec 27 '18 at 18:25
Dudo
175
175
Do not usex = (int *)malloc...
- usex = malloc..
– SergeyA
Dec 27 '18 at 18:42
add a comment |
Do not usex = (int *)malloc...
- usex = malloc..
– SergeyA
Dec 27 '18 at 18:42
Do not use
x = (int *)malloc...
- use x = malloc..
– SergeyA
Dec 27 '18 at 18:42
Do not use
x = (int *)malloc...
- use x = malloc..
– SergeyA
Dec 27 '18 at 18:42
add a comment |
2 Answers
2
active
oldest
votes
Scanning 2D array ? For that you need to take a
as of int**
type not just int*
type. For e.g
int **a = malloc(NUM_OF_ROW * sizeof(int*)); /* allocate memory dynamically for n rows */
And then allocate memory for each row for e.g
for (i=0; i<size; i++){
a[i] = malloc(NUM_OF_COLUMN * sizeof(int)); /* in each row how many column, allocate that much memory dynamically */
}
It worked! Thanks alot! I did not notice this little detail before hehe
– Dudo
Dec 27 '18 at 19:20
I'm glad that my answer helped you. Your welcome on stackoverflow.
– Achal
Dec 27 '18 at 19:26
add a comment |
This doesn't work:
*a = (int *)malloc(size * sizeof(int));
Because a
has type int *
so *a
has type int
, so it doesn't make sense to assign a pointer to that. You're also attempting to dereference a pointer which has not been initialized yet, invoking undefined behavior.
You need to define a
as an int **
:
int **a;
And assign to it directly on the first allocation, using sizeof(int *)
for the element size:
a = malloc(size * sizeof(int *));
Note also that you shouldn't cast the return value of malloc
.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53949303%2fscanning-to-2d-array-and-then-passing-it-to-function-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Scanning 2D array ? For that you need to take a
as of int**
type not just int*
type. For e.g
int **a = malloc(NUM_OF_ROW * sizeof(int*)); /* allocate memory dynamically for n rows */
And then allocate memory for each row for e.g
for (i=0; i<size; i++){
a[i] = malloc(NUM_OF_COLUMN * sizeof(int)); /* in each row how many column, allocate that much memory dynamically */
}
It worked! Thanks alot! I did not notice this little detail before hehe
– Dudo
Dec 27 '18 at 19:20
I'm glad that my answer helped you. Your welcome on stackoverflow.
– Achal
Dec 27 '18 at 19:26
add a comment |
Scanning 2D array ? For that you need to take a
as of int**
type not just int*
type. For e.g
int **a = malloc(NUM_OF_ROW * sizeof(int*)); /* allocate memory dynamically for n rows */
And then allocate memory for each row for e.g
for (i=0; i<size; i++){
a[i] = malloc(NUM_OF_COLUMN * sizeof(int)); /* in each row how many column, allocate that much memory dynamically */
}
It worked! Thanks alot! I did not notice this little detail before hehe
– Dudo
Dec 27 '18 at 19:20
I'm glad that my answer helped you. Your welcome on stackoverflow.
– Achal
Dec 27 '18 at 19:26
add a comment |
Scanning 2D array ? For that you need to take a
as of int**
type not just int*
type. For e.g
int **a = malloc(NUM_OF_ROW * sizeof(int*)); /* allocate memory dynamically for n rows */
And then allocate memory for each row for e.g
for (i=0; i<size; i++){
a[i] = malloc(NUM_OF_COLUMN * sizeof(int)); /* in each row how many column, allocate that much memory dynamically */
}
Scanning 2D array ? For that you need to take a
as of int**
type not just int*
type. For e.g
int **a = malloc(NUM_OF_ROW * sizeof(int*)); /* allocate memory dynamically for n rows */
And then allocate memory for each row for e.g
for (i=0; i<size; i++){
a[i] = malloc(NUM_OF_COLUMN * sizeof(int)); /* in each row how many column, allocate that much memory dynamically */
}
answered Dec 27 '18 at 18:30
Achal
9,1202730
9,1202730
It worked! Thanks alot! I did not notice this little detail before hehe
– Dudo
Dec 27 '18 at 19:20
I'm glad that my answer helped you. Your welcome on stackoverflow.
– Achal
Dec 27 '18 at 19:26
add a comment |
It worked! Thanks alot! I did not notice this little detail before hehe
– Dudo
Dec 27 '18 at 19:20
I'm glad that my answer helped you. Your welcome on stackoverflow.
– Achal
Dec 27 '18 at 19:26
It worked! Thanks alot! I did not notice this little detail before hehe
– Dudo
Dec 27 '18 at 19:20
It worked! Thanks alot! I did not notice this little detail before hehe
– Dudo
Dec 27 '18 at 19:20
I'm glad that my answer helped you. Your welcome on stackoverflow.
– Achal
Dec 27 '18 at 19:26
I'm glad that my answer helped you. Your welcome on stackoverflow.
– Achal
Dec 27 '18 at 19:26
add a comment |
This doesn't work:
*a = (int *)malloc(size * sizeof(int));
Because a
has type int *
so *a
has type int
, so it doesn't make sense to assign a pointer to that. You're also attempting to dereference a pointer which has not been initialized yet, invoking undefined behavior.
You need to define a
as an int **
:
int **a;
And assign to it directly on the first allocation, using sizeof(int *)
for the element size:
a = malloc(size * sizeof(int *));
Note also that you shouldn't cast the return value of malloc
.
add a comment |
This doesn't work:
*a = (int *)malloc(size * sizeof(int));
Because a
has type int *
so *a
has type int
, so it doesn't make sense to assign a pointer to that. You're also attempting to dereference a pointer which has not been initialized yet, invoking undefined behavior.
You need to define a
as an int **
:
int **a;
And assign to it directly on the first allocation, using sizeof(int *)
for the element size:
a = malloc(size * sizeof(int *));
Note also that you shouldn't cast the return value of malloc
.
add a comment |
This doesn't work:
*a = (int *)malloc(size * sizeof(int));
Because a
has type int *
so *a
has type int
, so it doesn't make sense to assign a pointer to that. You're also attempting to dereference a pointer which has not been initialized yet, invoking undefined behavior.
You need to define a
as an int **
:
int **a;
And assign to it directly on the first allocation, using sizeof(int *)
for the element size:
a = malloc(size * sizeof(int *));
Note also that you shouldn't cast the return value of malloc
.
This doesn't work:
*a = (int *)malloc(size * sizeof(int));
Because a
has type int *
so *a
has type int
, so it doesn't make sense to assign a pointer to that. You're also attempting to dereference a pointer which has not been initialized yet, invoking undefined behavior.
You need to define a
as an int **
:
int **a;
And assign to it directly on the first allocation, using sizeof(int *)
for the element size:
a = malloc(size * sizeof(int *));
Note also that you shouldn't cast the return value of malloc
.
edited Dec 27 '18 at 18:44
answered Dec 27 '18 at 18:29
dbush
92.9k12101133
92.9k12101133
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53949303%2fscanning-to-2d-array-and-then-passing-it-to-function-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Do not use
x = (int *)malloc...
- usex = malloc..
– SergeyA
Dec 27 '18 at 18:42