相关疑难解决方法(0)

指向指针的指针如何在C中工作?

如何指针指针在C中工作?你什么时候使用它们?

c

164
推荐指数
11
解决办法
12万
查看次数

如何修改已传递到C函数中的指针?

所以,我有一些代码,类似于以下,将结构添加到结构列表:

void barPush(BarList * list,Bar * bar)
{
    // if there is no move to add, then we are done
    if (bar == NULL) return;//EMPTY_LIST;

    // allocate space for the new node
    BarList * newNode = malloc(sizeof(BarList));

    // assign the right values
    newNode->val = bar;
    newNode->nextBar = list;

    // and set list to be equal to the new head of the list
    list = newNode; // This line works, but list only changes inside of this function
}
Run Code Online (Sandbox Code Playgroud)

这些结构定义如下:

typedef …
Run Code Online (Sandbox Code Playgroud)

c pointers pass-by-value

49
推荐指数
4
解决办法
5万
查看次数

用于多级指针解引用?

什么时候使用任何语言的指针要求有人使用多个指针,让我们说一个三指针.什么时候使用三指针而不是只使用常规指针是有意义的?

例如:

char  * * *ptr;
Run Code Online (Sandbox Code Playgroud)

代替

char *ptr;
Run Code Online (Sandbox Code Playgroud)

c++ pointers

42
推荐指数
4
解决办法
2万
查看次数

更改函数内的指针不会反映在函数外部

void alloco(int *ppa)
{
    int i;
    printf("inside alloco %d\n",ppa);
    ppa = (int *)malloc(20);
    ppa[15] = 9;
    printf("size of a %d \n", sizeof(ppa));
    for(i=0;i<20;i++)
    printf("a[%d] = %d \n", i, ppa[i]);
}

int main()
{
    int *app = NULL;
    int i;
    printf("inside main\n");
    alloco(app);
    for(i=0;i<20;i++)
    printf("app[%d] = %d \n", i, app[i]);
    return(0);
}
Run Code Online (Sandbox Code Playgroud)

基本上我想做的就是将一个空指针从my传递main给一个函数(alloco),它分配内存/填充指针所指向的相同位置并返回.我正确地获得了本地打印,这是在函数(alloco)内部但不是main.

我在这里做错了吗?

c pointers

2
推荐指数
2
解决办法
1885
查看次数

指针分配指针然后迭代导致分段错误

我遇到以下代码问题.我很确定这是正确的.我正在尝试将内存分配给函数中的指针,然后使用for循环将数据写入同一函数中的已分配内存.但是当for循环中的计数器达到2时,它会导致分段错误,或者我尝试在循环中设置一堆值或从中读取.

是的,我已经在StackOverflow上研究了这个,但我没有发现以下答案太有用了.*如何分配内存并将其(通过指针参数)返回给调用函数?

#include <cassert>
#include <cstdlib>
#include <cstdio>

typedef unsigned char uint8_t;

void allocMem(uint8_t **i){
    *i = new uint8_t[10];
    for(int j = 0; j < 10; j++){
        printf("%d\n", *i[j]);
        //*i[j] = j * 2;
    }
}

int main(){
    uint8_t *i = 0;
    allocMem(&i);
    assert(i != NULL);
    delete[] i;
}
Run Code Online (Sandbox Code Playgroud)

c++ pointers

1
推荐指数
1
解决办法
58
查看次数

标签 统计

pointers ×4

c ×3

c++ ×2

pass-by-value ×1