Thread: alocation of a matrix

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    167

    alocation of a matrix

    I have a matrix alocated dinamicaly:
    Code:
    int **alocmat(int n)
    {
            int **a,i=0;
            a=(int**)calloc(n,sizeof(int*));
            for(;i<n;i++) *(a+i)=calloc(n,sizeof(int));
    
            return a;
    }
    This code is OK!

    and now i try to read using pointers. the bolded line can work using a[i][j], but i want to write it using pointers. how can i do that?
    Code:
    void citire(int** a,int n)
    {
            int i=0,j;
            for(;i<n;i++)
                    for(j=0;j<n;j++)
                    {
                            printf("[%d][%d]: ",i,j);
                            scanf("%d",*(*(a+i)+j));
                    }
    }
    Thank you!

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    The problem is that scanf() expects the address of the location in which to store the data, but you're passing the data itself. Try just simply removing the first * so it would look like:
    Code:
    scanf("%d", *(a+i)+j);
    Or alternatively you could do this I guess, if it makes it more clear to you what's going on:
    Code:
    scanf("%d", &(*(*(a+i)+j)));
    Just like you would do:
    Code:
    int a;
    
    scanf("%d", &a);  // RIGHT!
    instead of:
    Code:
    int a;
    
    scanf("%d", a);  // WRONG!
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    167
    and if i want to type it will be:
    Code:
    void tipar(int** a,int n)
    {
            int i=0,j;
            for(;i<n;i++)
            {
                    for(j=0;j<n;j++) printf("%d ",*(*(a+i)+j));
                    printf("\n");
            }
    
    }
    thank you!
    good forum, good advice!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C - access violation
    By uber in forum C Programming
    Replies: 2
    Last Post: 07-08-2009, 01:30 PM
  2. Matrix Help
    By HelpmeMark in forum C++ Programming
    Replies: 27
    Last Post: 03-06-2008, 05:57 PM
  3. Matrix and vector operations on computers
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 05-11-2004, 06:36 AM
  4. Matrix Reloaded Questions (SPOILERS_
    By Xei in forum A Brief History of Cprogramming.com
    Replies: 73
    Last Post: 10-19-2003, 02:21 PM