Thread: Help with pointers

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    2

    Help with pointers

    Hi, thanks for taking the time to read this. I'm a fairly new C programmer having trouble comprehending this problem. I've already looked all over the web for help, to no avail.

    These are the errors I'm getting.

    rain_mod.c: In function ‘main’:
    rain_mod.c:29: error: array subscript is not an integer
    rain_mod.c:42: error: array subscript is not an integer

    I know pointers have to match the data type of what they point to, have to be initialized, and that they point to a memory location rather than a variable itself. But beyond that, things get a bit fuzzy. Thanks for any help offered.




    Code:
    #include<stdio.h>
    
    
    
    #define MONTHS 12
    
    #define YEARS 5
    
    
    
    int main(void)	{
    
    	const float rain[YEARS][MONTHS]=
    
    	{
    
    	      {4.3,4.3,4.3,3.0,2.0,1.2,0.2,0.2,0.4,2.4,3.5,6.6},
    
    	      {8.5,8.2,1.2,1.6,2.4,0.0,5.2,0.9,0.3,0.9,1.4,7.3},
    
    	      {9.1,8.5,6.7,4.3,2.1,0.8,0.2,0.2,1.1,2.3,6.1,8.4},
    
    	      {7.2,9.9,8.4,3.3,1.2,0.8,0.4,0.0,0.6,1.7,4.3,6.2},
    
          	      {7.6,5.6,3.8,2.8,3.8,0.2,0.0,0.0,0.0,1.3,2.6,5.2}		8
    
    	
    
    	};
    
    int year=0, month=0;
    
    int *year_ptr,*month_ptr;
    
    
    
    year_ptr=&year;
    
    month_ptr=&month;
    
    
    
    float subtot=0, total;
    
    
    
    printf(" YEAR	RAINFALL   (inches)\n");
    
    for(year=0, total=0; year < YEARS;year++)
    
    	{
    
    			for (month=0; month<MONTHS; month++)
    
    			{
    
    			subtot +=rain[year_ptr][month_ptr];
    
    			}
    
    	printf("%5d %15.1f\n",2000+year, subtot);
    
    	total += subtot;//total for all years	
    
    	}
    
    printf("\nThe yearly average is %.1f inches.\n\n",total/YEARS);
    
    printf("MONTHLY AVERAGES:\n\n");
    
    printf("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec\n");
    
    
    
    for(month=0;month<MONTHS;month++)
    
    	{//for each month, sum of rainfalls over the years
    
    		for(year=0; year<YEARS;year++)
    
    			{
    
    			subtot+=rain[year_ptr][month_ptr];	
    
    			}
    
    	printf("%4.1f ", subtot/YEARS);
    
    	}
    
    printf("\n");
    
    return 0;
    
    
    
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    +1 for using code tags
    -1 for double spacing it
    -1 for not highlighting the lines in question

    Why are you using pointers there?
    Code:
    int *year_ptr,*month_ptr;
    year_ptr=&year;
    month_ptr=&month;
    You are in the same scope as the actual varaiable, and you are changing the variables directly. I'm don't see the point of these variables.
    Code:
    			subtot +=rain[year_ptr][month_ptr];
    If you want to use pointers there, you need to be dereferencing them, not just sticking the pointer there.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    2
    Quote Originally Posted by quzah View Post
    +1 for using code tags
    -1 for double spacing it
    -1 for not highlighting the lines in question

    Why are you using pointers there?
    Code:
    int *year_ptr,*month_ptr;
    year_ptr=&year;
    month_ptr=&month;
    You are in the same scope as the actual varaiable, and you are changing the variables directly. I'm don't see the point of these variables.
    Code:
    			subtot +=rain[year_ptr][month_ptr];
    If you want to use pointers there, you need to be dereferencing them, not just sticking the pointer there.

    Quzah.
    Sorry, I'm still learning. This is an assignment to teach me about pointers. I wouldn't bother with them otherwise. In my next assignment, they will be out of scope. I dont know how to highlight lines here, but they're 29 and 42. Thanks for helping, I'll look it up.
    Last edited by xenu; 04-07-2011 at 03:06 PM.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Code:
    subtot +=rain[year_ptr][month_ptr];
    year_ptr and month_ptr contain an address (that's what a pointer variable is, an address which points to the memory that contains the value). In order to get the value pointed to by year_ptr you must use the dereference operator, *
    Code:
    subtot +=rain[*year_ptr][*month_ptr];
    A very good look at pointers and memory

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Arrays with pointers
    By Niels_M in forum C Programming
    Replies: 18
    Last Post: 07-31-2010, 03:31 AM
  2. size of struct with pointers and function pointers
    By sdsjohnny in forum C Programming
    Replies: 3
    Last Post: 07-02-2010, 05:19 AM
  3. Replies: 7
    Last Post: 05-19-2010, 02:12 AM
  4. pointers to arrays
    By rakeshkool27 in forum C Programming
    Replies: 1
    Last Post: 01-24-2010, 07:28 AM
  5. Pointers pointers pointers....
    By G'n'R in forum C Programming
    Replies: 11
    Last Post: 11-02-2001, 02:02 AM