I am learning C and pointers. I am following the code below and have a couple of questions.
My MS Visual Studio complains: uninitialized local variable 'day_ret' used. I then complied using Geany (another IDE) and it works. Is there something wrong with this code?
I feel the author who wrote the code should put some values to month and day. Otherwise, it will just print out the memory address, right? I want to kn开发者_C百科ow if I should put the initial value just after mian?
Reference: www.publications.gbdirect.co.uk/c_book/chapter5/pointers.html
#include <stdio.h>
#include <stdlib.h>
void date(int *, int *); /* declare the function */
int main(){
int month, day;
date (&day, &month);
printf("day is %d, month is %d\n", day, month);
exit(EXIT_SUCCESS);
}
void date(int *day_p, int *month_p){
int day_ret, month_ret;
/*
* At this point, calculate the day and month
* values in day_ret and month_ret respectively.
*/
*day_p = day_ret;
*month_p = month_ret;
}
Yes - you're missing the code in the comments:
/* * At this point, calculate the day and month * values in day_ret and month_ret respectively. */
That code would set
day_ret
andmonth_ret
. Without the missing code, it's effectively incomplete, and you could get any value forday
andmonth
.No, it's not going to print out pointers.
month
andday
are integer variables. Pointers to those variables are being passed to thedate
method, which is storing values via those pointers. The values are then being printed.
精彩评论