Array pointers question?

This is a discussion on Array pointers question? within the C Programming forums, part of the General Programming Boards category; Can somebody tell me how this can be changed to accept and print doubles. It works for only integers. I've ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    49

    Array pointers question?

    Can somebody tell me how this can be changed to accept and print doubles. It works for only integers. I've tried changing the int's to double and the%d to %f but it giving me errors

    Code:
    #include<stdio.h>
    #include<math.h>
    main(){    
    
                  int a,b,c;
    int i,j,k,p;
    int *aptr;
    int nrows, ncols; 
    printf("How many rows would you like store?\n ");
      scanf("%d", &nrows);
      printf("How many columns would you like to store?\n ");
      scanf("%d", &ncols);
      printf("Enter the number of nearest neighbours p: ");
      scanf("%d", &p);
    int row, col;
    int Total=nrows*ncols;
    int **rptr =(int**)malloc(nrows * sizeof(int *));
    if (rptr == NULL)
    {
    puts("\nFailure to allocate room for pointers");
    exit(0);
    }   
    
    /* we now allocate the memory for the array */
     aptr =(int*)malloc(Total* sizeof(int));
    if (aptr == NULL)
    {
    puts("\nFailure to allocate room for the array");
    exit(0);
    }
    /* and now we 'point' the pointers */
    for (k = 0; k < nrows; k++)
    {
    rptr[k] = aptr + (k * ncols);
    }
     for(i=0 ; i<Total; ++i) 
        {
        printf("Enter data row wise: ");
        scanf("%d", &aptr[i]);
        }
    printf("\n\nAnd now we print out the array\n");
    for (row = 0; row < nrows; row++)
    {
    for (col = 0; col < ncols; col++)
    {
    printf("%d \t", rptr[row][col]);}
    printf("\n\n");
    getchar();
    
           }   free(rptr);
           free(aptr);  }

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Well, not every int would become a double. For example, the variables holding the number of elements would still remain ints. What code did you end up with?

    There are other things wrong with the code. malloc() shouldn't be casted; stdlib.h should be included (for exit() and malloc() and free()); math.h doesn't need to be included; main should not use the implicit int rule and should return int. There are also things I would change but are not strictly wrong: declaring variables in the middle of a block is C99, and Total starts capitalized whereas the other variables do not.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    49
    Well I intend to store floating numbers with it. It works well with integers. The math.h I included was cos that's not the end of my program, I still have some other codes under this. But the data I want to work with has some floating digits in it, that's why I'm trying to see a way of accomodating it. Any suggestions?

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    This doesn't work? I've made all of my changes blue (except for the malloc casts, which I just removed).
    Code:
    #include<stdio.h>
    #include <stdlib.h>  /* added this and removed the malloc() casts */
    #include<math.h>
    main(){    
    
                  int a,b,c;
    int i,j,k,p;
    double *aptr;
    int nrows, ncols; 
    printf("How many rows would you like store?\n ");
      scanf("&#37;d", &nrows);
      printf("How many columns would you like to store?\n ");
      scanf("%d", &ncols);
      printf("Enter the number of nearest neighbours p: ");
      scanf("%d", &p);
    int row, col;
    int Total=nrows*ncols;
    double **rptr = malloc(nrows * sizeof(double *));
    if (rptr == NULL)
    {
    puts("\nFailure to allocate room for pointers");
    exit(0);
    }   
    
    /* we now allocate the memory for the array */
     aptr = malloc(Total* sizeof(double));
    if (aptr == NULL)
    {
    puts("\nFailure to allocate room for the array");
    exit(0);
    }
    /* and now we 'point' the pointers */
    for (k = 0; k < nrows; k++)
    {
    rptr[k] = aptr + (k * ncols);
    }
     for(i=0 ; i<Total; ++i) 
        {
        printf("Enter data row wise: ");
        scanf("%lf", &aptr[i]);
        }
    printf("\n\nAnd now we print out the array\n");
    for (row = 0; row < nrows; row++)
    {
    for (col = 0; col < ncols; col++)
    {
    printf("%f \t", rptr[row][col]);}
    printf("\n\n");
    getchar();
    
           }   free(rptr);
           free(aptr);  }
    BTW, your indentation could use some working on.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    49
    thanks for putting me through. Cheers!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array of Pointers to Arrays
    By Biozero in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 02:31 PM
  2. Replies: 4
    Last Post: 11-02-2006, 10:41 AM
  3. linked list inside array of structs- Syntax question
    By rasmith1955 in forum C Programming
    Replies: 14
    Last Post: 02-28-2005, 04:16 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 05:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21