使用指针从函数返回值的正确方法

时间:2014-08-31 02:58:28

标签: c pointers

除了一些用于AVR微控制器的简单嵌入式编码之外,我对C编程很陌生。

最近,我一直在尝试编写一个简单的环形缓冲区来接收串行数据,并找到了一些似乎在大多数情况下都能正常工作的示例代码。但是有一个指针传递给一个函数,用于从环形缓冲区返回值。不用说我努力理解指针。

我附加了在Pelles C中完成的所有代码,这似乎有效,但我不确定我是否正在处理来自*pc函数的int buf_get(char *pc)指针。我能够使用附加的代码编译没有错误或警告。

有人请告诉我为*pc设置变量的正确方法吗?

到目前为止,我正在使用char FromBuffer[1];,但我认为它充其量只是草率。

/****************************************************************************
 *                                                                          *
 * File    : main.c                                                         *
 *                                                                          *
 * Purpose : Console mode (command line) program.                           *
 *                                                                          *
 * History : Date      Reason                                               *
 *           8/28/2014 Ring Buffer Example                                  *
 *                                                                          *
 ***************************************************************************/

 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <conio.h>     //needed for _getch(),_getche() and _putch()
 #include <time.h>      //used by the random number generator to start seed based on time

 //============================= Constants ===================================
 #define BUFSIZE 16

//============================= Functions ===================================
void DumpBuffer(void);  //Used to display buffer for debugging
void buf_init(void);    //Ring Buffer initialization
int buf_put(char c);    //Ring Buffer add function
int buf_get(char *pc);  //Ring Buffer get fucntion

//============================= Global Variables=============================
char buf[BUFSIZE];          //ring buffer
char *pIn, *pOut, *pEnd;    //pointers for buffer fucntions
char full;                  //fucntion value for buffer functions and control within
char FromBuffer[1];
int BufferInputStatus;      //0 = FULL, 1 = OK
int BufferOutputStatus;     //0 = EMPTY, 1 = OK
long InputPointerBase;      //input pointer base value used during display of ring buffer
long OutputPointerBase;     //output pointer base value used during display of ring buffer

/****************************************************************************
 *                                                                          *
 * Function: main                                                           *
 *                                                                          *
 * Purpose : Main entry point.                                              *
 *                                                                          *
 * History : Date      Reason                                               *
 *           8/28/2014 Ring Buffer Example                                  *
 *                                                                          *
 ***************************************************************************/
/****************************************************************************
MAIN function that does:
1)
2)
****************************************************************************/
int main(int argc, char *argv[])
{
 char CharIn;
 int RandomChar = 97;       //used by random character generator
                        //int num_between_x_and_y = (rand() % (X - Y)) + Y;
 int LastRandomChar =0;     //used to prevent two random char repeats in a row

//tell the user the program has started
printf("Start \n");

//seed the random number generator
srand(time(NULL));

//initialize the ring buffer
buf_init();

//find the base address of the pointers
InputPointerBase = (long)(pIn);
OutputPointerBase = (long)(pOut);
printf("Base Address Input Pointer %x\n",(int)(InputPointerBase));
printf("Base Address Output Pointer %x\n",(int)(OutputPointerBase));


//Main loop that allows filling the buffer and removing from buffer
//User used "i" or "o" to add or remove to ring buffer
//User exits with "Q"
while ((CharIn = _getch()) != 'Q')  // "_getch()" does not wait for CR to return key and has no  cho
{
//add something to the input buffer
if (CharIn == 'i') 
    {
    RandomChar = (rand() % (122 - 97)) + 97;    //get a ramdom character

    //Only add to ring buffer is not a ramdom repeat
    if (RandomChar != LastRandomChar)
        {
        printf ("Adding to buffer ==> %c\n", RandomChar);
        LastRandomChar = RandomChar;
        BufferInputStatus = buf_put((char)(RandomChar));    //add character to ring buffer
        }
    }//end of IF "input"
//remove something from input buffer
if (CharIn == 'o')
    {
    BufferOutputStatus = buf_get(FromBuffer);
    }
//Show what is in the buffer along with the input and output pointers
DumpBuffer();

//Diaply what came out of the buffer
printf("This was the output : %c\n",FromBuffer[0]);

//printf("Input Pointing to %x\n",(int)(pIn));
if (BufferInputStatus == 1) printf("Buffer Input Status is OK\n");
    else printf("Buffer Input Status is FULL\n");

    if (BufferOutputStatus == 1) printf("Buffer Output Status is OK\n");
        else printf("Buffer Output Status is EMPTY\n");

    }//end of "While !='Q' "
 printf("Exit \n");
return 0;
}



void DumpBuffer(void)
 {
 char BufferLocation = 0;
 char InputPointerValue = 0;
 char OutputPointerValue = 0;

 //Display the buffer pointers and buffer content
for (BufferLocation = 0; BufferLocation < BUFSIZE; BufferLocation++)
  {
  //Show the location of the input pointer
  InputPointerValue = (char)(pIn - InputPointerBase);
  if (BufferLocation == InputPointerValue) printf("%-3s",">>>");
    else printf("%-3s","");

  //Show the buffer location
  printf(":%-3d:",BufferLocation);

  //Display what is in the buffer at that location
  printf(":%-3c:",buf[BufferLocation]);

  //Show the location of the output pointer
  OutputPointerValue = (char)(pOut - OutputPointerBase);
  if (BufferLocation == OutputPointerValue) printf("%-3s",">>>");
    else printf("%-3s","");

  //end the displayed line with a CR-LF
  printf("\n");
  }//End of FOR-LOOP for printing buffers
}//end of "DumpBuffer"


/****************************************************************************
* Raw example code from:                                                    *
* Example code from:                                                        *
* http://stackoverflow.com/questions/827691/how-do-you-implement-a-circular-buffer-in-c
*                                                                           *
*   No changes were made!!!!                                                *
*                                                                           *
****************************************************************************/

// init
void buf_init(void)
{
pIn = pOut = buf;       // init to any slot in buffer
pEnd = &buf[BUFSIZE];   // past last valid slot in buffer
full = 0;               // buffer is empty
}

// add char 'c' to buffer
int buf_put(char c)
{
if (pIn == pOut  &&  full)
    return 0;           // buffer overrun

*pIn++ = c;             // insert c into buffer
if (pIn >= pEnd)        // end of circular buffer?
    pIn = buf;          // wrap around

if (pIn == pOut)        // did we run into the output ptr?
    full = 1;           // can't add any more data into buffer
return 1;               // all OK
}

// get a char from circular buffer
int buf_get(char *pc)
{
if (pIn == pOut  &&  !full)
    return 0;           // buffer empty  FAIL

*pc = *pOut++;          // pick up next char to be returned
if (pOut >= pEnd)       // end of circular buffer?
    pOut = buf;         // wrap around

full = 0;               // there is at least 1 slot
return 1;               // *pc has the data to be returned
}

2 个答案:

答案 0 :(得分:2)

你问:

  

有人请告诉我为* pc设置变量的正确方法吗?

查看您的代码以及您如何使用FromBuffer,我会说:

  1. 完全摆脱FromBuffer
  2. main中,声明一个变量

    char fromBufferChar;
    
  3. 将您使用FromBuffer的两个地方的fromBufferChar替换为 BufferOutputStatus = buf_get(FromBuffer);

    更改

     BufferOutputStatus = buf_get(&fromBufferChar);
    

    printf("This was the output : %c\n",FromBuffer[0]);
    

    更改

    printf("This was the output : %c\n",fromBufferChar);
    

    {{1}}

答案 1 :(得分:1)

首先我们尝试一些简单的事情,使用指针为变量赋值:

char c;
char *p;
p = &c;
*p = 'x';

然后我们做同样的事情,但将指针传递给执行动作的函数:

void foo(char *a)
{
  *a = 'z';
}

...

char c;
char *p;
p = &c;
foo(p);

我们也可以取消不必要的指针变量:

char c;
foo(&c);

现在使用数组元素:

char m[5];
char *p;
p = &m[0];
*p = 'j';

数组变量的值是第一个元素的地址,因此我们可以这样做:

char m[5];
char *p;
p = m;
*p = 'j';

因此我们可以这样使用这个功能:

char m[5];
char *p;
p = m;
foo(p);

或者这样:

char m[5];
foo(m);

这能说清楚吗?