指向本地指针数组的全局指针 [英] Global pointer to local pointer array

查看:91
本文介绍了指向本地指针数组的全局指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个函数中,我有一个指向数组的指针数组.我需要在另一个函数中访问该数组.

I have an array of pointers to arrays, in a function. I need to access that array in another function.

指针数组

char *ptr[size + 1];

我做了一个全局指针

char *p;

我指的是ptr(与ptr具有相同的功能)

which i pointed to ptr (in the same function as ptr)

p = *ptr;

然后我尝试从其他功能访问ptr

then i tried to access ptr from the other function

void otherFunction(void){
    for(int n = 0; n < 6; n++)
    {
        char* str = &p[n];
        printf("%i %s\n", n, str);
    }
}

何时

ptr[0] = "abcd"
ptr[1] = "bcde"
ptr[2] = "cdef"
ptr[3] = "defg"
ptr[4] = "efgh"
ptr[5] = "fghi"

otherfunction(); 的输出是:

0 abcd
1 bcd
2 cd
3 d
4 
5 

我希望输出为

 0 abcd
 1 bcde
 2 cdef
 3 defg
 4 efgh
 5 fghi

我的问题是:(0)这里出了什么问题.(1)我该如何修复(2)还是有更好的方法来做到这一点.要求是 otherfunction()不能接受任何参数,并且 ptr 必须保持在其函数本地.(确定其他代码不会导致此问题,并且 ptr 没问题)

My questions are: (0) whats going wrong here. (1) How do i fix it (2) Or is there a better way to do this. the requirements are otherfunction() cant take any arguments and ptr needs to remain local to its function. (Im certain the other code is not contributing to the problem and nothing is wrong with ptr)

推荐答案

ptr 是指针数组.

char **p = ptr;

char *str = p[n];
printf("%i %s\n", n, str);

这篇关于指向本地指针数组的全局指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆