6
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

c言語_ポインタのポインタを関数に渡す

Last updated at Posted at 2017-04-26

ここ2年くらい、pythonばっかり触ってたので、c言語(特にポインタの使い方)忘れてしまいました。。
うんこですよね。。

main.c

#include <stdio.h>

void allocate(int **array, int size, int value){

    *array = (int*)malloc(size * sizeof(int));

    for (int i=0; i<size; i++){
        *(*array + i) = value; // unko
    }
}


int main(int argc, const char * argv[]) {

    int *v = NULL;
    int size = 5;
    int value = 100;

    allocate(&v, size, value);

    for (int i=0; i<size; i++){
        printf("v[%d]=%d\n", i,v[i]);
    }

    return 0;
}

int型のポインタvのポインタ&vをallocate関数に渡して
うんこするサンプルコードです。

./main.o

v[0]=100
v[1]=100
v[2]=100
v[3]=100
v[4]=100

参考:

詳説Cポインタ

  
  
どやっ!

6
3
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?