开发者

use pointer pass value

I am learning C and pointers. I am following the code below and have a couple of questions.

  1. 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?

  2. 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;
}


  1. 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 and month_ret. Without the missing code, it's effectively incomplete, and you could get any value for day and month.

  2. No, it's not going to print out pointers. month and day are integer variables. Pointers to those variables are being passed to the date method, which is storing values via those pointers. The values are then being printed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜