3

I'm really a beginner in C programming language, and i've started learning pointers... I have some problems manipulating them. What i want to do is to read and write a matrix, with 2 functions, without using global variables (just pointers)... So I did not succed making this. I've searched a lot about pointers and I try to understand how i can use them , but i'm not able to read and write that matrix What i do wrong ...Please, please, please help me (Even with some Useful links about 2DArray & pointers)...Thank you!

#include <stdio.h>
#include <stdlib.h>

void readM(int (*x)[100][100], int *row, int *column)
{
int i,j;
printf("Row no: ");
scanf("%d",row);
printf("Column no: ");
scanf("%d",column);
printf("Matrix elemnts: \n");    
for(i=0;i<(*row);i++)
{
    for(j=0;j<(*column);j++)
    {
        printf("[%d][%d]=",i,j);
        scanf("%d",x[i][j]);
    }
}
}

void writeM(int (*x)[][100], int *row, int *column)
{
int i,j;
printf("\nMatrix is: \n");
for(i=0;i<(*row);i++){
    for (j=0;j<(*column);j++){
    printf("%d",(*x)[i][j]);
    }
    printf("\n");
    }
}

int main()
{

char choice;
int a[100][100],m,n;
do
{
   printf("\nChose an option\n\n"
      "1) read matrix \n"
      "2) write matrix\n"
      "3) display matrix in spiral\n"
      "4) return max and min\n"
      ///...etc
   scanf("%c", &choice);
   while (choice<'0'|| choice>'4')
         {
            printf("\nInvalid option! Chose again! \n\n");
            scanf("%c",&choice);
         }
   switch (choice)
   {
        case '0': exit(0);
        case '1': readM(&a,&m,&n); break;
        case '2': writeM(&a,&m,&n);break; /// ... etc
} while (choice !=5);
getch();
}
ionut
  • 667
  • 1
  • 5
  • 14
  • 2
    There is a lot of cluttered code, it doesn't even compile, there are mismatched braces. Plus you have used language (excuse me) which only you understand. If it is not english, remove it from your code and paste on the section with problems. – fkl Nov 03 '12 at 20:14
  • Yes that is not English...Sorry... – ionut Nov 03 '12 at 20:34
  • 1
    Read section 6 of the [comp.lang.c FAQ](http://www.c-faq.com). – Keith Thompson Nov 03 '12 at 20:41
  • I think you should be able to read past strings and variable names that are not in English, and you can ignore comments that aren't in English. The code should be comprehensible without understanding the names. I agree that the copied code should be compilable and even the amended code is still not valid. – Jonathan Leffler Nov 03 '12 at 21:01
  • Always check that `scanf()` converted the number of values you expected: `if (scanf(...) != 3) { ...process error... }` if you try to read 3 values. This avoids a variety of problems. – Jonathan Leffler Nov 03 '12 at 21:03
  • By the way, is this language Finnish? –  Nov 03 '12 at 21:22
  • @H2CO3 No, not that. I'd guess Romanian or something similar (Moldavian?). – Daniel Fischer Nov 03 '12 at 22:48

2 Answers2

2

There probably would be more than one mistakes

Using scanf with %s for a single char variable. If you wanna input a char, use %c (better yet, use getc or getchar) since scanf has it's own issues. Or pass a char array to %s. But then you can't compare a string with a char like you did in

choice<'0'

Also remove the '\n' before scanf. In printf \n flushes the buffer and moves to next line. In case of scanf, adding it after the %c or %s still makes some sense (as you are indicating a terminator), but certainly not before it.

There is no function pointer here. They are used to pass functions as parameters. You are merely passing an array which is passed by reference by default. So simply use

int x[100][100] or int x[][100]

in parameters of function definitions and treat x as an array like

x[a][b] rather than *x[a][b]

Also when passing array from main simply pass 'a' which is the name of array that is passed by reference itself. Passing address of an array is meaningless.

fkl
  • 5,412
  • 4
  • 28
  • 68
  • ok ...thank you... My compiler doesn't return me any error or warning at that line so i did'nt see that is wrong. But my problem was with readM and writeM functions. – ionut Nov 03 '12 at 20:20
  • 1
    I have pointed out issues in them too. reload the answer. Also treating an error or warning as the only means of some thing wrong is very dangerous with c like language since it allows the freedom to do a lot. Don't write a line of code unless you understand what it really does – fkl Nov 03 '12 at 20:23
  • Thank you...I think that the problem it was that i don't undwestand what my teacher say. She ask me to use pointers in that functions....And that is the part i can not manage...I have to make also some more functions that return some sums and make other modifications on my matrix...It will work? – ionut Nov 03 '12 at 20:28
  • I've studied Pascal before, so transition to C it's quite difficult. – ionut Nov 03 '12 at 20:31
  • 2
    That's fine just try to grasp the full concept before using some thing. Or read about the syntax first. Using pointers in functions has absolutely nothing to do with function pointers. The latter are another type of pointers which instead of pointing to simple variables, point to functions and can be called using those pointers. The (*x) syntax you used is describing a function pointer with no meaning given the subjected scenario – fkl Nov 03 '12 at 20:34
  • :)) you're right That is Because I've searched a lot over internet and i found so many different informations about how to use pointers&co and i doesn't know what is correct and incorect to use ... – ionut Nov 03 '12 at 20:41
  • Good to hear that. If that resolves all your problems, perhaps you should accept the answer :) – fkl Nov 03 '12 at 21:39
2

Another issue is, when you are reading the array, you are first following the pointer (*x), and then adding the array offset [i][j]: that would likely get you to some memory location you don't even have access to. Since what you are passing is an array of pointers, you would look at the number location first (x[i][j]), and then follow the pointer.

Edit: the previous user updated his answer to reflect the second advice I gave here, so I erased it.

Alex
  • 947
  • 1
  • 8
  • 25
  • Yeah i saw your answer latter. I was editing his code and my answer along. Up voted since you mentioned it first. – fkl Nov 03 '12 at 20:27