Skip to content

Instantly share code, notes, and snippets.

@KentaYamada
Created July 18, 2016 09:33
Show Gist options
  • Select an option

  • Save KentaYamada/aa69f6675d21a194cd98a30289f1450a to your computer and use it in GitHub Desktop.

Select an option

Save KentaYamada/aa69f6675d21a194cd98a30289f1450a to your computer and use it in GitHub Desktop.
ポインタの使い方いろいろ
#include <stdio.h>
static void func(const int *val);
static void func2(const char *val);
static void func3(const char val[]);
static void func4(int *val);
static void func5(const char *val);
int main(void)
{
int a = 10;
char b = 'a';
char *c = "Hello, world.";
int d = 100;
//変数のアドレスを渡す場合
func(&a);
func2(&b);
func3(c);
printf("before val is %d\n", d); //100
func4(&d);
printf("after val is %d\n", d); //expected 222
func5(c);
return 0;
}
//変数のアドレスを受け取る場合は「*引数名」で定義する
static void func(const int *val)
{
//ポインタが指す実データを参照する
printf("val is %d\n", *val);
}
static void func2(const char *val)
{
printf("val is %c\n", *val);
}
static void func3(const char val[])
{
printf("val is %s\n", val);
}
static void func4(int *val)
{
*val = 222;
}
static void func5(const char *val)
{
printf("val is %s\n", val);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment