Thread: Passing pointer to array to a function

  1. #1
    Registered User
    Join Date
    May 2002

    Passing pointer to array to a function


    I've been tearing my hair out trying to pass an array to a function using pointers. I keep reading things that tell me an array is a pointer to a series of addresses, and then others that tell me this isn't quite true. So I have no idea what to do.

    I am a student trying to do a homework and just can't seem to figure this one out.

    The arrays are stu_count, stu_hours, and gpa.

    void get_input(int, int, float, int, int);
    float get_tot_hours(int, int);
    int get_stu_count(int);
    int rpt_out(int, int, float, int, int);
    void main(void)
    	int stu_hours[10] = {0};
    	int stu_num[10] = {0};
    	float gpa[10] = {0};
    	int stu_count = 0;
    	int finish = 1;
    	int *pfinish;
    	int *pscount;
    	pfinish = &finish;
    	*pfinish = 1;
    	pscount = &stu_count;
    	*pscount = 0;
    	while((stu_count < 11) && (finish != 0))
    		//get_input(psnum, stu_count, pgpa, pshours, pfinish); 
    		get_input(*stu_num, pscount, *gpa, *stu_hours, pfinish);
    	//rpt_out(get_tot_hours(stu_count, pshours), get_stu_count(psnum), pgpa, pshours, psnum);
    	rpt_out(get_tot_hours(stu_count, *stu_hours), get_stu_count(*stu_num), *gpa, *stu_hours, *stu_num);
    void get_input(int *stu_num, int stu_count, float *gpa, int *stu_hours, int *pfinish)
    	printf("Please Input the 3 digit student code, or 0 to finish:  ");
    	scanf("%d", stu_num[stu_count]);
    	if ((stu_num + stu_count) == 0)
    		pfinish = 0;
    	printf("\nPlease Input the hours for this student:  ");
    	scanf("%d", (stu_hours + stu_count));
    	printf("\nPlease Input this student's GPA:  ");
    	scanf("%f", (gpa + stu_count));
    float get_tot_hours(int stu_count, int stu_hours)
    	float tot_hours = 0.0f;
    	int ilocal = 0;
    	for(ilocal = 0; ilocal < stu_count; ilocal++)
    		if((stu_hours + ilocal) != 0)
    			tot_hours += (stu_hours + ilocal);
    	return tot_hours;
    int get_stu_count(int stu_num)
    	int ilocal = 0;
    	int total = 0;
    	for(ilocal = 0; ilocal < 11; ilocal++)
    	if((stu_num + ilocal) != 0)
    	return total;
    int rpt_out(int tot_hours, int stu_count, float gpa, int stu_hours, int stu_code)
    	int ilocal = 0;
    	printf("\nThe total hours for input students is:  %d", tot_hours);
    	printf("\nThe total number of students is:  %d", stu_count);
    	printf("\n\nStudent: \t Hours: \t GPA\n");
    	for (ilocal = 0; ilocal <= stu_count; ilocal++)
    		printf("\n%d \t\t %d \t\t %1.1f", (stu_code + ilocal), (stu_hours + ilocal), (gpa + ilocal));
    	return 0;
    Thank you for your help.


  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    get_input(*stu_num, pscount, *gpa, *stu_hours, pfinish);

    void get_input(int *stu_num, int stu_count, float *gpa, int *stu_hours, int *pfinish)
    The '*' dereferences a pointer.
    The '&' gives you the address.

    Your function requires a pointer, not the value located in the pointer. So, you want something like:

    get_input( stu_num, pscount, gpa, stu_hours, pfinish );

    When passing a pointer, not a pointer to a pointer, to a function that takes a pointer as an argument, just use the name. If you use '* name', you actually end up dereferencing the pointer which gives you the value contained in whatever the pointer points at. Example:

    void myfun( int *p );
    int x;
    int *ptr;

    ptr = &x;

    myfun( ptr );


    myfun( &x );


    myfun( *ptr );

    Or not...

    myfun( x );


    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing a pointer to two-dimension array in a function
    By E_I_S in forum C++ Programming
    Replies: 11
    Last Post: 06-19-2008, 09:57 AM
  2. Replies: 1
    Last Post: 03-24-2008, 10:16 AM
  3. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM