如何返回字符串变量c

问题描述:

我的程序试图模拟自动售货机。除硬币退还或退还零件以外的所有零件都可以使用。我试图把插入到机器中的美分量输入到主体外的另一个代码中,然后返回一个字符串。这在C中是可能的,因为我不断得到一个初始化使得整型指针没有转换错误。如果这是不可能的,关于如何做到这一点的任何想法?如何返回字符串变量c

谢谢!

#include <stdio.h> 
#include <stdlib.h> 
#include <stdbool.h> 
#define NI 5 
#define DI 10 
#define QU 25 



bool isValid(int num) { 
    return (num >= 10 && num <= 100); 
} 

bool isMultiple(int num) { 
     return (num % 5 == 0); 
} 


char *coinReturn(int insert) { 

           int dimes = insert/DI; 
           int remainder = insert % DI; 
           int nickels = remainder/NI; 
           int remainder2= nickels % NI; 
           char *m = ("Change returned. %d nickels and %d dimes\n",nickels, dimes); 
           return m; 
} 


int 
main (int argc, char *argv[]) 
{ for (int i = 1; i != argc; ++i) { 
     int price = atoi(argv[i]); 
     if (!isValid(price)) { 
      printf("Price muse be from 10 cents to 100 cents.\n"); 
      break; 
     } else if (!isMultiple(price)) { 
       printf("Price must be a multiple of 5.\n"); 
       break; 
     } else { 
       printf(" Welcome to my Vending Machine!\n"); 
       printf("Pop is %d cents. Please enter nickels, dimes, or quarters\n", price); 

         int cents = 0; 
         char coin = '\0'; 

         while (cents <= price && coin != 'E') { 
         printf(" PLease enter a coin [NDQR]\n"); 
         scanf (" %c", &coin); 

         if (coin == 'N'|| coin == 'n') { 
           cents = cents + NI; 
           printf(" You have inserted 5 cents\n"); 
         } 
         else if (coin == 'd' || coin == 'D') { 
           cents = cents + DI; 
           printf("You have inserted 10 cents\n"); 
         } 
         else if (coin == 'Q' || coin == 'q') { 
           cents = cents + QU; 
           printf("You have entered 25 cents\n"); 
         } 
         else if (coin == 'R' || coin == 'r'){ 
           printf("Change requested\n"); 
           char *rtn2= coinReturn(cents); 
           printf("%s\n",rtn2); 
         } else { 
           printf("Unknown coin. Rejected.\n"); 
         } 
         int balance = price - cents; 
         printf("You have entered a total of %d cents\n", cents); 

         if (balance > 0) { 
         printf("You must enter %d more cents\n", balance); 
        } else { 

           char *rtn = coinReturn(cents); 
           printf("%s\n",rtn); 
         cents = 0; 

        } 

        } 


       printf("DONE!\n"); 
       return 0; 
} 
} 

这行不通:

字符* M =( “更改返回%d镍和%d硬币\ n”。,镍,角钱);

也许摆脱char *并只是调用printf?

+0

但我需要以某种方式返回声明。我可以用printf来做到这一点? – JVAN

+0

不是。你可以'malloc'缓冲区,使用['snprintf'](http://www.cplusplus.com/reference/cstdio/snprintf/)格式化它,然后返回它。 –

+0

但你所做的所有回报是printf它。为什么不直接在函数中执行printf? –

它实际上可以做你在说什么。以下是您可能会喜欢的两种解决方案:

解决方案1:使用printf而不是返回。

首先,你没有真正写

char *m = ("Change returned. %d nickels and %d dimes\n", nickels, dimes);

我们需要将这些值传递给函数来进行实际的格式格式化您的字符串!

其次,我不明白为什么我们需要从coinReturn返回......当我们可以简单地写

printf("Change returned. %d nickels and %d dimes\n", nickels, dimes);

,我们就大功告成了!

但也许这不是我们想要的。也许我们要真正返回一个字符串指针这个功能,我们可以做到这一点太:)

解决方案2:使用的snprintf并返回一个指向静态缓冲区

char *coinReturn(int insert) { 
    // set aside a buffer for our string. 
    static char buf[512]; 
    int dimes = insert/DI; 
    int remainder = insert % DI; 
    int nickels = remainder/NI; 
    int remainder2= nickels % NI; 
    // output our string into the buffer. 
    snprintf(buf, sizeof(buf), 
     "Change returned. %d nickels and %d dimes\n", nickels, dimes); 
    return buf; 
} 

首先我们设置留出一定的空间存储我们的格式化字符串,

static char buf[512];

注意它的neccesary声明bufstatic所以它在我们的coinReturn函数的范围内幸存下来。

512将是我们的最大字符串长度,在这里应该足够了。

让我们来看看,明年部分:

snprintf(buf, sizeof(buf), 
    "Change returned. %d nickels and %d dimes\n", nickels, dimes); 

(参见:https://linux.die.net/man/3/printf

第一个参数是我们的字符串将被写入,buf位置。

第二个参数是字节snprintf的最大数目将写(包括空终止)。

一个参数是一样printf,你已经很熟悉。

return buf; 

终于可以回到buf知道结果会是一个指向我们的格式化字符串。


出这两种解决方案,第一个肯定是更有意义 - 有很多我看到#2(例如:它不是线程安全的)问题,但它得到是工作如果做这就是你想要的。