标签: pointer-to-pointer

我可以存储对本地参考的引用吗?

要清楚,我想要一个指向指针的行为,这个问题的目的是生成干净,可读的代码.

我有一些代码包含检查多个Dictionary.TryGetValue调用结果的条件.如果它可以通过一次调用检索所有必需的对象,那将会更清晰,所以我想编写一个扩展,允许我执行以下操作:

Dictionary<string, string> myDictionary; // Initialized somewhere

string x, y, z;
bool foundAllEntries = myDictionary.TryGetValues({"xvalue", out x}, {"yvalue", out y}, 
    {"zvalue", out z});
if (foundAllEntries)
    ; // Do something with x, y, and z
Run Code Online (Sandbox Code Playgroud)

但是,我无法找到一种方法将扩展方法引用传递给将保存输出的对象.这似乎应该是非常基本的东西.

如何在对象中存储对本地引用的引用?

请注意,这个问题并不是要求实现TryGetValues函数的替代方法.我可以通过很多方式实现"工作",但没有一种方法可以像我想要的那样生成干净的代码.

c# reference pointer-to-pointer

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

为什么处理对象经常出现为指向指针的指针

将句柄的句柄设置为指针指针但不是指针的意图是什么?如下代码:

FT_Library library;
FT_Error error = FT_Init_FreeType( &library );
Run Code Online (Sandbox Code Playgroud)

哪里

typedef struct FT_LibraryRec_  *FT_Library
Run Code Online (Sandbox Code Playgroud)

所以&library是一个FT_LIBraryRec_类型的句柄FT_LIBraryRec_**

c pointers handle pointer-to-pointer

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

将多维数组传递给C中的函数

我有这样的功能:

void myfunc(int** arr, int n) {
  int i, j;
  for(i=0; i<n; ++i) {
    for(j=0; j<n; ++j) {
      printf("%d,", *(arr + i*n + j) );  // Print numbers with commas
    }
    printf("\n");                        // Just breakline
  }
}
Run Code Online (Sandbox Code Playgroud)

在其他函数中我有一个二维数组,如下所示:

int main() {
  int seqs[8][8] = {
    {0, 32, 36, 52, 48, 16, 20, 4},
    {0, 16, 20, 52, 48, 32, 36, 4},
    {0, 32, 36, 44, 40, 8, 12, 4},
    {0, 8, 12, 44, 40, 32, 36, 4},
    {0, 32, 36, …
Run Code Online (Sandbox Code Playgroud)

c pointers function multidimensional-array pointer-to-pointer

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

malloc和指针指针

我正在尝试理解何时需要在使用多级指针时使用malloc.例如,

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

int main() {
    typedef struct {
        char first[10];
        char last[10];
    } Person;

    Person *p;

    p = malloc(sizeof(Person));
    strcpy(p->first, "John");
    strcpy(p->last, "Doe");

    printf("First: %s Last:%s\n", p->first, p->last);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

在我正在使用的第一个版本中Person *p,我只使用malloc为类型分配空间Person.在第二个版本中,我将Person *p改为Person **p

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

int main() {
    typedef struct {
        char first[10];
        char last[10];
    } Person;

    Person **p;

    *p = malloc(sizeof(Person));
    strcpy((*p)->first, "John");
    strcpy((*p)->last, "Doe");

    printf("First: %s Last:%s\n", (*p)->first, …
Run Code Online (Sandbox Code Playgroud)

c malloc pointers pointer-to-pointer

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

带有文本的char *是否像使用malloc一样自动保留内存?

// Example program
#include <iostream>
#include <string>

using namespace std;

int main()
{
    char **p;
    p = (char **)malloc(100);
    p[0] = (char *)"Apple";      // or write *p, points to location of 'A'
    p[1] = (char *)"Banana";     // or write *(p+1), points to location of 'B'

    cout << *p << endl;          //Prints the first pointer 
}
Run Code Online (Sandbox Code Playgroud)

在上面的代码中:

p[0] = (char *)"Apple"; 
Run Code Online (Sandbox Code Playgroud)

似乎会自动保留内存。没有malloc。这是C / C ++标准还是特定于编译器?

更新1 我实际上对它在C中然后在C ++中的状态很感兴趣。只是我没有为上述代码安装C编译器,因此我使用了C ++。

那么在STACK上分配p指向HEAP中的一个内存块(数组),其中每个元素都指向(是一个指针)指向DATA段中的文字?哇!

c++ pointers pointer-to-pointer

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

这是一个指向数组开头指针的指针吗?

我刚刚用某些代码帮助了某人.他有这个:

char dataArray[10];
Run Code Online (Sandbox Code Playgroud)

然后想得到一个指向数组开头的指针.而不是使用:

&dataArray[0]
Run Code Online (Sandbox Code Playgroud)

要不就

dataArray
Run Code Online (Sandbox Code Playgroud)

他曾经

&dataArray
Run Code Online (Sandbox Code Playgroud)

他最后用指针指向那里吗?我不确定&dataArray会给他什么.

c arrays pointer-to-pointer

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

当我们声明双指针时,究竟是什么int/char(**q)

让我们采取64位机器

其中指针在64位机器中是8个字节

int *p ; // it is a pointer to integer variable so when i increment p
         // i.e., p++ it will increment by 4 

char *r; // It is pointer to character . 
         // So if i increment 'r' it will increment by 1

int **q ; // if i increment q  ie.,q++ it will increment 8 bytes 
Run Code Online (Sandbox Code Playgroud)

我试过这个代码的和平如果有任何错误请纠正我

int a=10; 
int *p; 
char *r; 
int **q; 
p=&a; 
q=&p; 
printf("p= %p\t r= %p\t q=%p\n",p,r,q);
 printf("p(increment)= %p\t r (increment)= …
Run Code Online (Sandbox Code Playgroud)

c types pointers pointer-to-pointer

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

使用 -&gt; 运算符访问指向结构指针的指针

我有这个代码:

#include<stdio.h>
#include<stdlib.h>

struct node
{
    int data;
    struct node *next;
};

void pointerOfPointer(struct node **reference)
{
    struct node *temporary = malloc(sizeof(struct node));

    temporary->data = 100;
    temporary->next = 0;

    printf("before: temporary->data %d\n", temporary->data);
    temporary = *reference;
    printf("after: temporary->data %d\n", temporary->data);
}

int main()
{
    struct node *linkedlist = malloc(sizeof(struct node));
    linkedlist->data = 15;
    linkedlist->next = 0;
    pointerOfPointer(&linkedlist);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

如何在不将 *reference 地址复制到临时局部变量的情况下访问 pointerOfPointer 函数中指向结构指针的指针?所以最后我可以直接使用operator -> 访问引用变量数据,比如reference->data?

c struct pointers pointer-to-pointer

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

双指针的 malloc 分段错误

我正在制作一个程序,从文件中读取文本,将其分隔为字母数字单词,等等。我遇到了一个问题,我从文件中读入一个字符串,我试图将该字符串分隔成一个二维数组,其中每个单词都存储在一个数组中。从那里我将继续订购它们。

但是,当我为 word[n] 分配 malloc 时,我遇到了段错误。对于我正在使用的测试文件,它有大约 400 个字符。没有比说 10 个字符更长的词了,所以 (j - i + 1) 不会是一个可能导致堆栈溢出的巨大分配(除非我误解了我的分配)。

如果需要,我可以解释我的函数代码,它是对我编写的 strsplit() 函数的操作。

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char** argv) {
    if (argc != 2) {
        printf("Please provide 1 argument.\n");
        return 0;
    }

    FILE *file = fopen(argv[1], "r");
    fseek(file, 0, SEEK_END);
    size_t length = ftell(file); // Getting file size

    char buff, **words, buffer[length + 1];
    int i = 0, j = 0, n = 0, h = 0;

    buffer[length] = '\0';
    rewind(file); …
Run Code Online (Sandbox Code Playgroud)

c malloc segmentation-fault pointer-to-pointer

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

为什么这个指针做作会引发错误?

最近开始研究指针,想知道为什么char *n = “hii”; char *p = &n;会显示error

#指向地址#

#include<stdio.h>
#include<string.h>
#include<cs50.h>


int main(void)
   {
        char *n = "hii";
        /*why the below line is showing the error as it has no impact on the result, below the pointer p is pointing to address of n*/
        char *p = &n;
        printf("%i\n", &n);              

   }
  
Run Code Online (Sandbox Code Playgroud)

c pointers declaration pointer-to-pointer

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