我不断收到有关传递参数的警告,使指针从整数开始而没有强制转换

时间:2018-09-07 00:40:54

标签: c function pointers printf

C:\ Users \ OWNER \ Downloads \ program1.c | 61 |警告:传递'get_range'的参数1会使指针从整数开始而无需强制转换|

巨大的问题将非常有助于教授拒绝提供帮助。 晚饭后我一直盯着这个问题大约三个小时。我仍然不知道。如果您可以帮我解释一下,以便我可以了解自己在做什么,我将不胜感激。

/**********************************************************************/
/*                                                                    */
/* Program Name: program1                                             */
/* Author:       Tommy Lokey                                          */
/* Course:       CS 227, Data Structures and Algorithms               */
/* Date Written: September 2, 2018                                    */
/*                                                                    */
/**********************************************************************/

/**********************************************************************/
/*                                                                    */
/* I pledge  all of the lines  in this C program  are my own original */
/* work and that none of the lines in this C program have been copied */
/* from anyone else, unless I was specifically authorized to do so by */
/*                        my CS227 instructor.                        */
/*                                                                    */
/*                                                                    */
/*                      Signed: _____________________________________ */
/*                                           (signature)              */
/*                                                                    */
/*                                                                    */
/**********************************************************************/

/**********************************************************************/
/*                                                                    */
/* This program asks the user for two wholes numbers, it then sums    */
/* the range of the two whole numbers. It then displays the sum.      */
/*                                                                    */
/**********************************************************************/

#include <stdio.h> /* printf, scanf                                   */

/**********************************************************************/
/*                         Symbolic Constants                         */
/**********************************************************************/
#define COURSE_NUMBER  "CS227"    /* PCC assigned course number       */
#define PROGRAM_NUMBER 1          /* Teacher assigned program number  */
#define USER_NAME      "Lokey" /* The Programmer's username        */

/**********************************************************************/
/*                        Function Prototypes                         */
/**********************************************************************/

void print_heading();
/* Print the program heading*/
void get_range(int *p_low_number, int *p_high_number);
int sum_range(int low_number, int high_number);

/**********************************************************************/
/*                         Main Function                              */
/**********************************************************************/
int main()
{
    int low_number,  /* Minimum number entered by user                 */
        high_number; /* Maximum number entered by user                 */

     /* Print the program heading                                       */
     printf("\n\n\n\n\n\n");
     print_heading();
     get_range(low_number, high_number);
     printf("\nThe sum of all you numbers between %d and", low_number);
     printf("%d (inclusively) is %d", high_number,
      sum_range(low_number, high_number));
     printf("\nThanks for your range summing. Have a nice day! ;-)");
     return 0;
    }


   /**********************************************************************/
   /*                   Print the program heading                        */
   /**********************************************************************/
   void print_heading()
   {
   printf("\n========================================================");
   printf("\n                Program Number: %d",     PROGRAM_NUMBER);
   printf("\n                Programmer: %s",         USER_NAME);
   printf("\n                PCC Course Number: %s",  COURSE_NUMBER);
   printf("\n========================================================");
   printf("\nI'll sum whole numbers in a number range (inclusively)");
   printf("\n--------------------------------------------------------");
   return;
   }

    /**********************************************************************/    
    /*                        Get Range Function                          */
    /**********************************************************************/
    void get_range(int *p_low_number, int *p_high_number)
    {
     printf("\nEnter a range of whole numbers now, lower number first:");
     printf("\n  What is your small number: ");
     scanf(" %d", p_low_number);
     printf("\n  Give your large number: ");
     scanf(" %d", p_high_number);
     return;
    }

    /**********************************************************************/
    /*                        Sum Range Function                          */
    /**********************************************************************/
    int sum_range(int low_number, int high_number)
    {
      int range_counter = 0;

      for(low_number; low_number <= high_number; low_number++)
    {
      range_counter = range_counter + low_number;
    }
    return range_counter;
 }

3 个答案:

答案 0 :(得分:1)

function commas(num::Integer) str = string(num) return replace(str, r"(?<=[0-9])(?=(?:[0-9]{3})+(?![0-9]))" => ",") end println(commas(123456789)) println(commas(123)) println(commas(123456789123456789123456789123456789)) """ Output 123,456,789 123 123,456,789,123,456,789,123,456,789,123,456,789 """ => get_range(low_number, high_number);

答案 1 :(得分:0)

函数get_range()需要两个指针。这是因为它将使用这些指针将值分配给另一个函数的变量。

您的教授希望您使用此函数来读入main()中声明的两个变量。因此,您必须使用这些变量的地址,并将其传递给get_range(),而不是它们的值。

答案 2 :(得分:0)

您传递的是错误的参数伴侣!您在integer pointers声明中接受get_range,但在函数调用中传递值。

在您的情况下,类型转换是从integerinteger指针。在某些情况下,该函数采用空指针来容纳所有数据类型。 在这种情况下,您需要将参数类型转换为(void *)

这将是大多数编写良好的模块化函数中的函数声明:

int CheckIfIn(char ch, void *checkstring);

如果元音不是字符指针,则需要将参数作为空指针传递

 if(CheckIfIn(letter, (void *)Vowels) ){
        printf("this is a vowel.\n");
    }