Thread: input with 2D array and pointers

  1. #1
    Registered User
    Join Date
    Feb 2004
    Posts
    73

    input with 2D array and pointers

    I am working on a menu driven program for entering grades, etc... The part I having trouble with is the imput. I am trying to use pointer notation to input the grades in a 2D array. 10 rows, 5 columns. So 10 students, and 5 grades each. This is what I have so far, but I dont know what notation to use for imputing into the elements of the 2D array.

    Code:
    int studentgrades [10][5]={0};
    int *i;
    i = studentgrades;
    
    /* Enter grades for a student */
    int enter_grades(){
          int a, b;
    
          printf ("Enter student number you want to enter grades for: ");
          scanf ("%d", &a);
    
          printf ("Enter grades for student number %d: ", a);
          scanf ("%d %d %d %d %d", *(i + (a + 0)));
    
          printf ("Thank you!");
    }
    This is just one of the functions for one of the options. Thats all i posted causae thats all that is necessary.

  2. #2
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    maybe something like :
    Code:
    int j;
    printf ("Enter grades for student number %d: ", a);
    for (j = 0; j < 5; j++)
      scanf("%d",&i[a][j]);

  3. #3
    Registered User
    Join Date
    Feb 2004
    Posts
    73
    That isn't pointer notation though. Thats what I am looking for, pointer notation,

  4. #4
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    sorry, didn't read your post correctly
    try:
    Code:
    int j;
    for (j = 0; j<5; j++)
      scanf("%d",*(i+a)+j);

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >That isn't pointer notation though.
    Yes it is, you just can't see it because the indices are obstructing your view through the brackets.
    My best code is written with the delete key.

  6. #6
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    there is a pretty good tutorial on the subject here

  7. #7
    Registered User
    Join Date
    Feb 2004
    Posts
    73
    Hmm. I am getting a parse error before the , for some reason. This is what I have in terms of the individual function:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int studentgrades [10][5]={0};
    int a;
    
    /* Enter grades for a student */
    int enter_grades(){
          int j;
    
          printf ("Enter student number you want to enter grades for: ");
          scanf ("%d", &a);
    
          printf ("Enter grades for student number %d: ", a);
          for (j = 0; j<5; j++){
              scanf ("%d ",((studentgrades+a)+j);}
    
          printf ("Thank you!");
    }

  8. #8
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    Code:
      ...
      scanf ("%d",*(studentgrades+a)+j);
      ...
      return 0;
    }

  9. #9
    Registered User
    Join Date
    Feb 2004
    Posts
    73
    That didn't help.....so confusing

    EDIT. Nevermind. I have a ( error. Fixed. thanks.
    Last edited by pxleyes; 03-25-2004 at 02:43 PM.

  10. #10
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    well it works for me, i thought you are getting parse error here:
    Code:
    scanf ("%d ",((studentgrades+a)+j);

  11. #11
    Registered User
    Join Date
    Feb 2004
    Posts
    73
    Originally posted by viaxd
    well it works for me, i thought you are getting parse error here:
    Code:
    scanf ("%d ",((studentgrades+a)+j);
    That be correct sir.

  12. #12
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    int studentgrades [10][5]={0};
    I seem to remember something about Prelude saying for 2 dimensional arrays you need to use something else along with the
    Code:
    {0}
    anyone remember. Prelude what about you?
    p.s. sorry about the code tags on the cast it wouldn't let me post without them

  13. #13
    Insomniac
    Join Date
    Mar 2004
    Posts
    35
    Isn't it better to use extra sets of curlies when using a multidimensional array. It helps you to keep track of which values are assigned to which array.

    Example:
    Code:
    int New_Array[2][3] = { { 1, 2, 3},  {1, 2, 3} }

  14. #14
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I seem to remember something about Prelude saying for 2 dimensional arrays you need to use something else along with the
    It depends on what you want to initialize the array with. If all you want is an array with the equivalent of 0 in every element then
    Code:
    T array[size] = {0};
    Is what you want. On the other hand, if you want any other type of value you need to specify them exactly:
    Code:
    T array[size] = {0,1,2,3,4,5};
    Any unspecified values will be given T's equivalent of 0.

    For multidimensional arrays, it helps to format the list with redundant braces to help with readability:
    Code:
    T array[2][size] = {
      {0,1,2,3,4,5},
      {5,4,3,2,1,0}
    };
    It gets even more important as you add more dimensions.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 10-21-2007, 07:44 AM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM