c - 如何在 C 中实现 next() PHP 函数

标签 c arrays pointers

我尝试执行 next() C 中的 PHP 函数。如果我有

与我的实现不同的是我想用更多的点来完成这项工作。例如:

如果我有两个 char * 像:

char * a = "ac\0";
char * b = "bd\0";

然后我打电话:

printf("%c", cgetnext(a));
printf("%c", cgetnext(b));
printf("%c", cgetnext(a));
printf("%c", cgetnext(b));

得到如下输出:abcd

但我得到了 abab

这是我的代码:

`#include <string.h>
#include <stdlib.h>

typedef struct 
{
    int pLocation;
    int myindex;
    int lastIndex;
}   POINTERINFORMATION;

int                 __myIndex               = 0;
int                 pointersLocationsLength = 0;
char                * temparr               = NULL;
POINTERINFORMATION  pointersLocations[256];


int 
    plAdd (int p)
    {
        if (pointersLocationsLength >= sizeof(pointersLocations)) {
            return -1;
        } else {
            pointersLocations[pointersLocationsLength].pLocation = p;
            pointersLocations[pointersLocationsLength].lastIndex = 0;
            pointersLocationsLength ++;
            return pointersLocationsLength;
        }
    }

int 
    getPointer (int p, POINTERINFORMATION * out)
    {
        int i;
        for (i = 0; i < pointersLocationsLength; i++)
        {
            if(pointersLocations[pointersLocationsLength].pLocation == p)
            {
                pointersLocations[i].myindex = i;
                *out = pointersLocations[i];
                return 1;
            }
        }

        return 0;
    }

void 
    getPointerIndex(char ** variable, int * val)
    {
        char * buf = malloc(256);
        if(sprintf(buf,"%p", &variable) > 0){
            *val = strtol(buf, NULL, 16 );
        } else {
            *val = -1;
        }
    }

int 
    inArrayOfPointers (int pointer) 
    {
        POINTERINFORMATION pi;
        return getPointer(pointer, &pi);

    }

char 
    cgetnext(char * arr) 
    {
        char * myarr; 
        const size_t size = sizeof(char *) + 1;
        int pof;

        myarr = malloc(size);
        getPointerIndex (&arr, &pof);

        if (inArrayOfPointers(pof)){
            POINTERINFORMATION pi;

            if (getPointer(pof, &pi))
            {
                myarr = (char *)*(int *)pi.pLocation;
                __myIndex = pi.lastIndex;
                ++pointersLocations[pi.myindex].myindex;
            } else {
                return 0;
            }

        } else {

            if (plAdd(pof) == -1) {
                printf(" CANNOT ADD ELEMENT TO ARRAY\n");
                exit(0);
            } else {
                myarr = arr;
                __myIndex = 0;
            }
        }

        if (strlen(myarr) == __myIndex) {
            return 0;
        } else  {
            temparr = malloc(size);
            temparr = strdup(myarr);

            return myarr[__myIndex];
        }
    }`

如何解决这个问题?非常感谢解决它的不同解决方案!提前致谢。

最佳答案

如果您对 char*“数组”特别感兴趣,那么一个简单的解决方案可能就是增加指针。请注意,如果使用动态分配的内存,您需要保存原始指针以释放它。同样的想法也可以用于其他数据类型。

这是我的意思的一个例子。 cgetnext 此处仅返回数组中当前位置的字符并递增指针(通过地址传递)。

char cgetnext( char **p )
{
   assert( p != NULL );
   // check for end of string
   if ( **p == '\0' )
      return '\0';
   return *(*p)++;
}


int main( int argc, char* argv[] )
{
   char *a = "ac";
   char *b = "bd";

   printf( "%c", cgetnext(&a));
   printf( "%c", cgetnext(&b));
   printf( "%c", cgetnext(&a));
   printf( "%c", cgetnext(&b));

}

关于c - 如何在 C 中实现 next() PHP 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9627039/

相关文章:

c - Pthread 参数

c - C语言从键盘读取字符串

c - 切换大量 case 与单个 if

ios - tableView reloadData cellForRowAtIndexPath 未调用

c++ - 类方法变量,如果我将它们存储在类本身中会更快吗?

javascript - 如何将数组连接到对象数组内的数组?

c - 输出一个指针

c - 向数千个文件添加 trec 格式标签

c - 将指针传递给结构体数组

c - gcc:你能把函数指针放到不同的部分(不是.data)吗?