在函数内部分配指向数组文字的指针[复制]

时间:2021-02-12 21:20:52

This question already has an answer here:

这个问题在这里已有答案:

I have the following code:

我有以下代码:

void dereference_pointer(int* pointer){
  pointer = (int[]){1, 2, 3};

  printf("%d | %d | %d\n", pointer[0], pointer[1], pointer[2]);
  // This prints "1 | 2 | 3", like I wanted
}

int main(void) {
  int *pointer;

  pointer = malloc(sizeof(int)*3);
  dereference_pointer(pointer);

  printf("%d | %d | %d\n", pointer[0], pointer[1], pointer[2]);
  // This prints "0 | 0 | 0", essentially undoing what happened inside the function

  return 0;
}

I'm not entirely sure pointer = (int[]){1, 2, 3}; is the correct way of pointing a pointer to a new array, but that's the only way I could do it that wouldn't give me a type error.

我不完全确定pointer =(int []){1,2,3};是指向新数组的指针的正确方法,但这是我能做到的唯一方法,它不会给我一个类型错误。

2 个解决方案

#1


2  

Assigning pointer inside dereference_pointer doesn't change pointer outside the function, since it was sent by value. You could change it into:

在dereference_pointer中分配指针不会更改函数外部的指针,因为它是按值发送的。您可以将其更改为:

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

// Note the ** instead of *
void dereference_pointer(int** pointer){
  *pointer = (int[]){1, 2, 3}; // note dereferencing here
  //                        and here       and here      and here  :)
  printf("%d | %d | %d\n", (*pointer)[0], (*pointer)[1], (*pointer)[2]);
  // This prints "1 | 2 | 3"
}

int main(void) {
  int *pointer;

  pointer = malloc(sizeof(int)*3);
  dereference_pointer(&pointer); // by reference 

  printf("%d | %d | %d\n", pointer[0], pointer[1], pointer[2]);
  // This prints "0 | 0 | 0"
  return 0;
}

This should fix the 0 | 0 | 0 you get in the end.

这应该修复0 | 0 | 0到最后。


As for this:

至于这个:

I'm not entirely sure pointer = (int[]){1, 2, 3};

我不完全确定pointer =(int []){1,2,3};

This is fine. Your pointer is pointing to an array which is ok. When the function returns, the you will be left with a pointer with the correct values.

这可以。你的指针指向一个没问题的数组。当函数返回时,您将获得一个具有正确值的指针。

You could instead have used this beauty :-P:

你可以改用这个美女:-P:

void dereference_pointer(int** pointer){
  **pointer=1, *(*pointer+1)=2, *(*pointer+2)=3;

  printf("%d | %d | %d\n", (*pointer)[0], (*pointer)[1], (*pointer)[2]);
  // This prints "1 | 2 | 3"
}

but your way is a one-liner and more readable IMO.

但你的方式是一个单行且更易读的IMO。

#2


1  

I'm not entirely sure pointer = (int[]){1, 2, 3}; is the correct way of pointing a pointer to a new array, but that's the only way I could do it that wouldn't give me a type error.

我不完全确定pointer =(int []){1,2,3};是指向新数组的指针的正确方法,但这是我能做到的唯一方法,它不会给我一个类型错误。

The pointer passed into the function is the array. This is because arrays, when passed as arguments, decay into a pointer to the first element. Therefore, you will need to pass the number of elements in the array. (Knowing the start address of the array doesn't tell you anything about the end).

传递给函数的指针是数组。这是因为当作为参数传递时,数组会衰变为指向第一个元素的指针。因此,您需要传递数组中的元素数。 (知道数组的起始地址并没有告诉你有关结束的任何信息)。

Example:

void print(int *data, size_t n)
{
    for (size_t i = 0; i < n; ++i)
        printf("%d\n", data[i];
}

In fact, the function signature could also have been written as

实际上,函数签名也可以写成

int data[], size_t n

The caller code could look something like this:

调用者代码可能如下所示:

int *data = malloc(n * sizeof(int));
/* populate the dynamically allocated array */
print(data, n);

Or it could look like this:

或者它看起来像这样:

int data[MAX];
/* populate array */
print(data, MAX);

#1


2  

Assigning pointer inside dereference_pointer doesn't change pointer outside the function, since it was sent by value. You could change it into:

在dereference_pointer中分配指针不会更改函数外部的指针,因为它是按值发送的。您可以将其更改为:

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

// Note the ** instead of *
void dereference_pointer(int** pointer){
  *pointer = (int[]){1, 2, 3}; // note dereferencing here
  //                        and here       and here      and here  :)
  printf("%d | %d | %d\n", (*pointer)[0], (*pointer)[1], (*pointer)[2]);
  // This prints "1 | 2 | 3"
}

int main(void) {
  int *pointer;

  pointer = malloc(sizeof(int)*3);
  dereference_pointer(&pointer); // by reference 

  printf("%d | %d | %d\n", pointer[0], pointer[1], pointer[2]);
  // This prints "0 | 0 | 0"
  return 0;
}

This should fix the 0 | 0 | 0 you get in the end.

这应该修复0 | 0 | 0到最后。


As for this:

至于这个:

I'm not entirely sure pointer = (int[]){1, 2, 3};

我不完全确定pointer =(int []){1,2,3};

This is fine. Your pointer is pointing to an array which is ok. When the function returns, the you will be left with a pointer with the correct values.

这可以。你的指针指向一个没问题的数组。当函数返回时,您将获得一个具有正确值的指针。

You could instead have used this beauty :-P:

你可以改用这个美女:-P:

void dereference_pointer(int** pointer){
  **pointer=1, *(*pointer+1)=2, *(*pointer+2)=3;

  printf("%d | %d | %d\n", (*pointer)[0], (*pointer)[1], (*pointer)[2]);
  // This prints "1 | 2 | 3"
}

but your way is a one-liner and more readable IMO.

但你的方式是一个单行且更易读的IMO。

#2


1  

I'm not entirely sure pointer = (int[]){1, 2, 3}; is the correct way of pointing a pointer to a new array, but that's the only way I could do it that wouldn't give me a type error.

我不完全确定pointer =(int []){1,2,3};是指向新数组的指针的正确方法,但这是我能做到的唯一方法,它不会给我一个类型错误。

The pointer passed into the function is the array. This is because arrays, when passed as arguments, decay into a pointer to the first element. Therefore, you will need to pass the number of elements in the array. (Knowing the start address of the array doesn't tell you anything about the end).

传递给函数的指针是数组。这是因为当作为参数传递时,数组会衰变为指向第一个元素的指针。因此,您需要传递数组中的元素数。 (知道数组的起始地址并没有告诉你有关结束的任何信息)。

Example:

void print(int *data, size_t n)
{
    for (size_t i = 0; i < n; ++i)
        printf("%d\n", data[i];
}

In fact, the function signature could also have been written as

实际上,函数签名也可以写成

int data[], size_t n

The caller code could look something like this:

调用者代码可能如下所示:

int *data = malloc(n * sizeof(int));
/* populate the dynamically allocated array */
print(data, n);

Or it could look like this:

或者它看起来像这样:

int data[MAX];
/* populate array */
print(data, MAX);