Scanning to 2d array and then passing it to function - C












0














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)









share|improve this question






















  • Do not use x = (int *)malloc... - use x = malloc..
    – SergeyA
    Dec 27 '18 at 18:42
















0














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)









share|improve this question






















  • Do not use x = (int *)malloc... - use x = malloc..
    – SergeyA
    Dec 27 '18 at 18:42














0












0








0







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)









share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 27 '18 at 18:25









Dudo

175




175












  • 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
















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












2 Answers
2






active

oldest

votes


















1














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 */
}





share|improve this answer





















  • 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



















3














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.






share|improve this answer























    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    1














    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 */
    }





    share|improve this answer





















    • 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
















    1














    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 */
    }





    share|improve this answer





















    • 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














    1












    1








    1






    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 */
    }





    share|improve this answer












    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 */
    }






    share|improve this answer












    share|improve this answer



    share|improve this answer










    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


















    • 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













    3














    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.






    share|improve this answer




























      3














      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.






      share|improve this answer


























        3












        3








        3






        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.






        share|improve this answer














        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.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Dec 27 '18 at 18:44

























        answered Dec 27 '18 at 18:29









        dbush

        92.9k12101133




        92.9k12101133






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Monofisismo

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas