Segmentation fault during input of a matrix












-2














So, I have to create a program that, when the user inserts a table, has to send in output only the border of the table. I used a matrix for the input but when the user ends the input, the program stops and I get segmentation fault.



#include <stdio.h>
#define VAL 50

void inputMatrice(int m[VAL][VAL], int *ncol, int *nrig);
void bordi(int m[VAL], int ncol, int nrig, int v, int *nv);
void outputVettore(int v, int nv);

int main(){
int m[VAL][VAL], v[VAL], *ncol, *nrig, nc, nr, nv;
ncol = &nc;
nrig = &nr;
inputMatrice(m, ncol, nrig);
nv=0;
printf("ciao");
bordi(m, nc, nr, v, &nv);
outputVettore(v, nv);

}
/*Funzione per input matrici*/

void inputMatrice(int m[VAL][VAL], int *ncol, int *nrig){

int i=0, j=0;
printf("Inserisci il numero di righe: ");
scanf("%d", nrig);
printf("Inserisci il numero di colonne: ");
scanf("%d", ncol);
for(i=0;i<*nrig;i++){
for(j=0;j<*ncol;j++){
printf("Inserisci i valori della tabella nella posizione %d,%d: ", i+1, j+1);
scanf("%d", &m[i][j]);
}
}
}

void bordi(int m[VAL], int ncol, int nrig, int v, int *nv){
int i=0, j=0, k=0;
for(j=1; j<ncol; j++){
v[k]=m[i][j];
*nv++;
}
for(i=1;i<nrig;i++){
v[k] = m[i][j];
*nv++;
}
for(j=(ncol-1); j>0; j++){
v[k]=m[i][j];
*nv++;
}
for(i=(nrig-1); j>0; i++){
v[k]=m[i][j];
*nv++;
}
}
void outputVettore(int v, int nv){
int i=0;
for(i=0;i<nv;i++){
printf("|%d|", v[i]);
}

}


Sorry for the italian Output and for the bad english.










share|improve this question




















  • 2




    *nv++ increments the pointer, not the value that it points to. Try (*nv)++;
    – user3386109
    18 hours ago












  • i've tried it but i still get segmentation fault.
    – Danilo
    18 hours ago
















-2














So, I have to create a program that, when the user inserts a table, has to send in output only the border of the table. I used a matrix for the input but when the user ends the input, the program stops and I get segmentation fault.



#include <stdio.h>
#define VAL 50

void inputMatrice(int m[VAL][VAL], int *ncol, int *nrig);
void bordi(int m[VAL], int ncol, int nrig, int v, int *nv);
void outputVettore(int v, int nv);

int main(){
int m[VAL][VAL], v[VAL], *ncol, *nrig, nc, nr, nv;
ncol = &nc;
nrig = &nr;
inputMatrice(m, ncol, nrig);
nv=0;
printf("ciao");
bordi(m, nc, nr, v, &nv);
outputVettore(v, nv);

}
/*Funzione per input matrici*/

void inputMatrice(int m[VAL][VAL], int *ncol, int *nrig){

int i=0, j=0;
printf("Inserisci il numero di righe: ");
scanf("%d", nrig);
printf("Inserisci il numero di colonne: ");
scanf("%d", ncol);
for(i=0;i<*nrig;i++){
for(j=0;j<*ncol;j++){
printf("Inserisci i valori della tabella nella posizione %d,%d: ", i+1, j+1);
scanf("%d", &m[i][j]);
}
}
}

void bordi(int m[VAL], int ncol, int nrig, int v, int *nv){
int i=0, j=0, k=0;
for(j=1; j<ncol; j++){
v[k]=m[i][j];
*nv++;
}
for(i=1;i<nrig;i++){
v[k] = m[i][j];
*nv++;
}
for(j=(ncol-1); j>0; j++){
v[k]=m[i][j];
*nv++;
}
for(i=(nrig-1); j>0; i++){
v[k]=m[i][j];
*nv++;
}
}
void outputVettore(int v, int nv){
int i=0;
for(i=0;i<nv;i++){
printf("|%d|", v[i]);
}

}


Sorry for the italian Output and for the bad english.










share|improve this question




















  • 2




    *nv++ increments the pointer, not the value that it points to. Try (*nv)++;
    – user3386109
    18 hours ago












  • i've tried it but i still get segmentation fault.
    – Danilo
    18 hours ago














-2












-2








-2







So, I have to create a program that, when the user inserts a table, has to send in output only the border of the table. I used a matrix for the input but when the user ends the input, the program stops and I get segmentation fault.



#include <stdio.h>
#define VAL 50

void inputMatrice(int m[VAL][VAL], int *ncol, int *nrig);
void bordi(int m[VAL], int ncol, int nrig, int v, int *nv);
void outputVettore(int v, int nv);

int main(){
int m[VAL][VAL], v[VAL], *ncol, *nrig, nc, nr, nv;
ncol = &nc;
nrig = &nr;
inputMatrice(m, ncol, nrig);
nv=0;
printf("ciao");
bordi(m, nc, nr, v, &nv);
outputVettore(v, nv);

}
/*Funzione per input matrici*/

void inputMatrice(int m[VAL][VAL], int *ncol, int *nrig){

int i=0, j=0;
printf("Inserisci il numero di righe: ");
scanf("%d", nrig);
printf("Inserisci il numero di colonne: ");
scanf("%d", ncol);
for(i=0;i<*nrig;i++){
for(j=0;j<*ncol;j++){
printf("Inserisci i valori della tabella nella posizione %d,%d: ", i+1, j+1);
scanf("%d", &m[i][j]);
}
}
}

void bordi(int m[VAL], int ncol, int nrig, int v, int *nv){
int i=0, j=0, k=0;
for(j=1; j<ncol; j++){
v[k]=m[i][j];
*nv++;
}
for(i=1;i<nrig;i++){
v[k] = m[i][j];
*nv++;
}
for(j=(ncol-1); j>0; j++){
v[k]=m[i][j];
*nv++;
}
for(i=(nrig-1); j>0; i++){
v[k]=m[i][j];
*nv++;
}
}
void outputVettore(int v, int nv){
int i=0;
for(i=0;i<nv;i++){
printf("|%d|", v[i]);
}

}


Sorry for the italian Output and for the bad english.










share|improve this question















So, I have to create a program that, when the user inserts a table, has to send in output only the border of the table. I used a matrix for the input but when the user ends the input, the program stops and I get segmentation fault.



#include <stdio.h>
#define VAL 50

void inputMatrice(int m[VAL][VAL], int *ncol, int *nrig);
void bordi(int m[VAL], int ncol, int nrig, int v, int *nv);
void outputVettore(int v, int nv);

int main(){
int m[VAL][VAL], v[VAL], *ncol, *nrig, nc, nr, nv;
ncol = &nc;
nrig = &nr;
inputMatrice(m, ncol, nrig);
nv=0;
printf("ciao");
bordi(m, nc, nr, v, &nv);
outputVettore(v, nv);

}
/*Funzione per input matrici*/

void inputMatrice(int m[VAL][VAL], int *ncol, int *nrig){

int i=0, j=0;
printf("Inserisci il numero di righe: ");
scanf("%d", nrig);
printf("Inserisci il numero di colonne: ");
scanf("%d", ncol);
for(i=0;i<*nrig;i++){
for(j=0;j<*ncol;j++){
printf("Inserisci i valori della tabella nella posizione %d,%d: ", i+1, j+1);
scanf("%d", &m[i][j]);
}
}
}

void bordi(int m[VAL], int ncol, int nrig, int v, int *nv){
int i=0, j=0, k=0;
for(j=1; j<ncol; j++){
v[k]=m[i][j];
*nv++;
}
for(i=1;i<nrig;i++){
v[k] = m[i][j];
*nv++;
}
for(j=(ncol-1); j>0; j++){
v[k]=m[i][j];
*nv++;
}
for(i=(nrig-1); j>0; i++){
v[k]=m[i][j];
*nv++;
}
}
void outputVettore(int v, int nv){
int i=0;
for(i=0;i<nv;i++){
printf("|%d|", v[i]);
}

}


Sorry for the italian Output and for the bad english.







c






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 18 hours ago









Joey Mallone

3041419




3041419










asked 18 hours ago









Danilo

114




114








  • 2




    *nv++ increments the pointer, not the value that it points to. Try (*nv)++;
    – user3386109
    18 hours ago












  • i've tried it but i still get segmentation fault.
    – Danilo
    18 hours ago














  • 2




    *nv++ increments the pointer, not the value that it points to. Try (*nv)++;
    – user3386109
    18 hours ago












  • i've tried it but i still get segmentation fault.
    – Danilo
    18 hours ago








2




2




*nv++ increments the pointer, not the value that it points to. Try (*nv)++;
– user3386109
18 hours ago






*nv++ increments the pointer, not the value that it points to. Try (*nv)++;
– user3386109
18 hours ago














i've tried it but i still get segmentation fault.
– Danilo
18 hours ago




i've tried it but i still get segmentation fault.
– Danilo
18 hours ago












3 Answers
3






active

oldest

votes


















0














The error occurs in
void bordi(int m[VAL], int ncol, int nrig, int v, int *nv) function.



Fix 3rd and 4th for loops conditions. You incremented j in first loop but didn't decrement or set it to 0, so you get an infinite loop.






share|improve this answer





















  • It actually worked, i can't upvote you so I hope a thanks is ok.
    – Danilo
    18 hours ago



















0














The line in your program *nv++; increments the pointer which is totally wrong in logic, try incrementing the value .
that is, Try :-
(*nv)++;






share|improve this answer





















  • i've tried that but i still get segmentation fault.
    – Danilo
    18 hours ago










  • please mention what compiler you are using.
    – VISHNU PRASAD
    16 hours ago



















0














The problem is in function bordi(). Look at this for loop:



for(j=(ncol-1); j>0; j++){
v[k]=m[i][j];
*nv++;
}


The loop control variable j initialize with ncol-1 and checking for condition j>0. After executing the loop statements, the j will be incremented by 1 (j++). So the loop condition j>0 will always true [until the j overflows] and after few iterations your program starts accessing invalid value of array m (m[i][j]) which is causing the segmentation fault.



To fix this problem, replace j++ with j-- in the loop.



The same problem exists in the next for loop as well but in a little different way:



for(i=(nrig-1); j>0; i++){
v[k]=m[i][j];
*nv++;
}


here, in the loop condition you are using variable j.

You need to do two changes in this for loop.



for(i=(nrig-1); i>0; i--){
^^ ^^





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%2f53943034%2fsegmentation-fault-during-input-of-a-matrix%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    The error occurs in
    void bordi(int m[VAL], int ncol, int nrig, int v, int *nv) function.



    Fix 3rd and 4th for loops conditions. You incremented j in first loop but didn't decrement or set it to 0, so you get an infinite loop.






    share|improve this answer





















    • It actually worked, i can't upvote you so I hope a thanks is ok.
      – Danilo
      18 hours ago
















    0














    The error occurs in
    void bordi(int m[VAL], int ncol, int nrig, int v, int *nv) function.



    Fix 3rd and 4th for loops conditions. You incremented j in first loop but didn't decrement or set it to 0, so you get an infinite loop.






    share|improve this answer





















    • It actually worked, i can't upvote you so I hope a thanks is ok.
      – Danilo
      18 hours ago














    0












    0








    0






    The error occurs in
    void bordi(int m[VAL], int ncol, int nrig, int v, int *nv) function.



    Fix 3rd and 4th for loops conditions. You incremented j in first loop but didn't decrement or set it to 0, so you get an infinite loop.






    share|improve this answer












    The error occurs in
    void bordi(int m[VAL], int ncol, int nrig, int v, int *nv) function.



    Fix 3rd and 4th for loops conditions. You incremented j in first loop but didn't decrement or set it to 0, so you get an infinite loop.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered 18 hours ago









    TimBeam

    1297




    1297












    • It actually worked, i can't upvote you so I hope a thanks is ok.
      – Danilo
      18 hours ago


















    • It actually worked, i can't upvote you so I hope a thanks is ok.
      – Danilo
      18 hours ago
















    It actually worked, i can't upvote you so I hope a thanks is ok.
    – Danilo
    18 hours ago




    It actually worked, i can't upvote you so I hope a thanks is ok.
    – Danilo
    18 hours ago













    0














    The line in your program *nv++; increments the pointer which is totally wrong in logic, try incrementing the value .
    that is, Try :-
    (*nv)++;






    share|improve this answer





















    • i've tried that but i still get segmentation fault.
      – Danilo
      18 hours ago










    • please mention what compiler you are using.
      – VISHNU PRASAD
      16 hours ago
















    0














    The line in your program *nv++; increments the pointer which is totally wrong in logic, try incrementing the value .
    that is, Try :-
    (*nv)++;






    share|improve this answer





















    • i've tried that but i still get segmentation fault.
      – Danilo
      18 hours ago










    • please mention what compiler you are using.
      – VISHNU PRASAD
      16 hours ago














    0












    0








    0






    The line in your program *nv++; increments the pointer which is totally wrong in logic, try incrementing the value .
    that is, Try :-
    (*nv)++;






    share|improve this answer












    The line in your program *nv++; increments the pointer which is totally wrong in logic, try incrementing the value .
    that is, Try :-
    (*nv)++;







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered 18 hours ago









    VISHNU PRASAD

    244




    244












    • i've tried that but i still get segmentation fault.
      – Danilo
      18 hours ago










    • please mention what compiler you are using.
      – VISHNU PRASAD
      16 hours ago


















    • i've tried that but i still get segmentation fault.
      – Danilo
      18 hours ago










    • please mention what compiler you are using.
      – VISHNU PRASAD
      16 hours ago
















    i've tried that but i still get segmentation fault.
    – Danilo
    18 hours ago




    i've tried that but i still get segmentation fault.
    – Danilo
    18 hours ago












    please mention what compiler you are using.
    – VISHNU PRASAD
    16 hours ago




    please mention what compiler you are using.
    – VISHNU PRASAD
    16 hours ago











    0














    The problem is in function bordi(). Look at this for loop:



    for(j=(ncol-1); j>0; j++){
    v[k]=m[i][j];
    *nv++;
    }


    The loop control variable j initialize with ncol-1 and checking for condition j>0. After executing the loop statements, the j will be incremented by 1 (j++). So the loop condition j>0 will always true [until the j overflows] and after few iterations your program starts accessing invalid value of array m (m[i][j]) which is causing the segmentation fault.



    To fix this problem, replace j++ with j-- in the loop.



    The same problem exists in the next for loop as well but in a little different way:



    for(i=(nrig-1); j>0; i++){
    v[k]=m[i][j];
    *nv++;
    }


    here, in the loop condition you are using variable j.

    You need to do two changes in this for loop.



    for(i=(nrig-1); i>0; i--){
    ^^ ^^





    share|improve this answer


























      0














      The problem is in function bordi(). Look at this for loop:



      for(j=(ncol-1); j>0; j++){
      v[k]=m[i][j];
      *nv++;
      }


      The loop control variable j initialize with ncol-1 and checking for condition j>0. After executing the loop statements, the j will be incremented by 1 (j++). So the loop condition j>0 will always true [until the j overflows] and after few iterations your program starts accessing invalid value of array m (m[i][j]) which is causing the segmentation fault.



      To fix this problem, replace j++ with j-- in the loop.



      The same problem exists in the next for loop as well but in a little different way:



      for(i=(nrig-1); j>0; i++){
      v[k]=m[i][j];
      *nv++;
      }


      here, in the loop condition you are using variable j.

      You need to do two changes in this for loop.



      for(i=(nrig-1); i>0; i--){
      ^^ ^^





      share|improve this answer
























        0












        0








        0






        The problem is in function bordi(). Look at this for loop:



        for(j=(ncol-1); j>0; j++){
        v[k]=m[i][j];
        *nv++;
        }


        The loop control variable j initialize with ncol-1 and checking for condition j>0. After executing the loop statements, the j will be incremented by 1 (j++). So the loop condition j>0 will always true [until the j overflows] and after few iterations your program starts accessing invalid value of array m (m[i][j]) which is causing the segmentation fault.



        To fix this problem, replace j++ with j-- in the loop.



        The same problem exists in the next for loop as well but in a little different way:



        for(i=(nrig-1); j>0; i++){
        v[k]=m[i][j];
        *nv++;
        }


        here, in the loop condition you are using variable j.

        You need to do two changes in this for loop.



        for(i=(nrig-1); i>0; i--){
        ^^ ^^





        share|improve this answer












        The problem is in function bordi(). Look at this for loop:



        for(j=(ncol-1); j>0; j++){
        v[k]=m[i][j];
        *nv++;
        }


        The loop control variable j initialize with ncol-1 and checking for condition j>0. After executing the loop statements, the j will be incremented by 1 (j++). So the loop condition j>0 will always true [until the j overflows] and after few iterations your program starts accessing invalid value of array m (m[i][j]) which is causing the segmentation fault.



        To fix this problem, replace j++ with j-- in the loop.



        The same problem exists in the next for loop as well but in a little different way:



        for(i=(nrig-1); j>0; i++){
        v[k]=m[i][j];
        *nv++;
        }


        here, in the loop condition you are using variable j.

        You need to do two changes in this for loop.



        for(i=(nrig-1); i>0; i--){
        ^^ ^^






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 17 hours ago









        H.S.

        4,7731318




        4,7731318






























            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%2f53943034%2fsegmentation-fault-during-input-of-a-matrix%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