Thread: Pointers, Arrays, Assigning values

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    35

    Pointers, Arrays, Assigning values

    I have zero idea why this is occuring, I have looked at so many examples of pointers and arrays and I just cannot figure this out.

    All I am trying to do right now is create an n by m matrix and populate it with random numbers...but all I get are null pointers (ie 0)

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "mpi.h"
    
    int main(int argc, char* argv[]){
            int rank;
            int p;
            int i, j;
    
            double *A;              //Matrix A
            double *b;              //Vector b
    
            int m = atoi(argv[1]);
            int n = atoi(argv[2]);
    
            //printf("M=%d, N=%d\n", m,n);  //Strictly used for testing purposes
    
            A = malloc((n*m)*sizeof(double));
    
            for (i=0; i<n; i++){
                    for(j=0; j<n; j++){
                            A[i*n + j] =  rand() % 100;//assigns *A random numbers between 0 - 99
                            printf("%d\n",A[i*n +j]);
                    }
            }
    
            //genMatrix(n, m, A);
            /*
            MPI_Init(&argc, &argv);
            MPI_Comm_rank(MPI_COMM_WORLD, &rank);
            MPI_Comm_size(MPI_COMM_WORLD, &p);
            MPI_Finalize();
            */
    
            return 0;
    }
    I even tried writing
    Code:
    double *A;
    ...
    A = malloc((n*m)*sizeof(double);
    ...
    A[0] = 5;
    printf("%d", A[0]);
    And this is returning 0, rather, a null pointer as well. Any help would be greatly appreciated

  2. #2
    Novice
    Join Date
    Jul 2009
    Posts
    568
    srand() before calling rand() and:
    Code:
    for(j=0; j<n; j++);  // change to: for (j = 0; j < m; j++);
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    35
    I found the solution to my error...

    I am supposed to use %.1f....not %d

    >.<

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you're using gcc as your compiler, then seriously consider adding at least -Wall as a command line option.

    One of the many things it can diagnose is mis-matched printf/scanf formats.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers of chars and arrays of chars Question
    By shiroaisu in forum C++ Programming
    Replies: 9
    Last Post: 08-09-2010, 10:42 AM
  2. Arrays with pointers
    By Niels_M in forum C Programming
    Replies: 18
    Last Post: 07-31-2010, 03:31 AM
  3. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Help understanding arrays and pointers
    By James00 in forum C Programming
    Replies: 2
    Last Post: 05-27-2003, 01:41 AM

Tags for this Thread