0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

私が書いたC言語のポインタコードをChatGPTに解説してもらった

0
Last updated at Posted at 2026-05-15

昔、C言語のポインタの見本として、自分のメモ用に書いたコードをChatGPTに評価してもらったら、ばっさり切られたので、その記録を記す。

※禁止パターン書いてあるので実行は自己責任でお願いします。

ポインタ理解に当たって覚えておいた方がいいこと。C言語のポインタは、アドレスを意識すると理解しやすくなる。

C言語コード

point.c
/* ポインタ見本帳 */
#include <stdio.h>
int main(void)
{ 

//スタック上にchar型のポインタを確保し、 
//char型のポインタに定数領域上のhogeの場所を格納している。
char *str_ptr,*str_kara;
//配列strをスタック領域に確保しhoge\0を格納
char str[10]="hoge";

char str_array[256];
char *str_array_ptr[1];

str_ptr = &str[0];
printf("%lu\n",sizeof(str_ptr)); //4 ポインタのサイズ nullなし
printf("%lu\n",sizeof(str)); //5
printf("%lu\n",sizeof(*str_ptr)); //1文字のみ
printf("%lu\n",sizeof(str_array)); //256
printf("%lu\n",sizeof(str_array_ptr)); //1024 (256*4)? 4(1*4?)
printf("%lu\n",sizeof(char)); //1

//型が一致しないといわれる。
//ダブルクォーテーションは文字列
//シングルクォーテーションは1文字

//ポインタ1
//OK 
//出力:aoge,aoge
str_ptr[0] = 'a';
printf("%s\n",str_ptr);

//ポインタ2
//OK
//出力:boge,boge
*str_ptr = 'b';
printf("%s,%p,%p\n",str_ptr,&str_ptr,&str);

while(*str_ptr != '\0') 
{
putchar(*str_ptr);
++str_ptr; 
}

printf("\n");

//禁止
str_kara = str_ptr;
printf("%s\n",str_kara); 

//配列
//OK
//出力:fgge,fgge
str[0] = 'f'; str[1] = 'g'; printf("%s,%s,%p,%p\n",str_ptr,str,&str_ptr,&str);

//ANSI規格では、文字列リテラルの変更は未定義(何が起こるかわからないということ)
//禁止
str_ptr = "ab";
printf("%s,%s,%p,%p\n",str_ptr,str,&str_ptr,&str); 

//文字列後の結果
//NG
//出力:ab,dego
str[0] = 'd'; str[1] = 'e'; printf("%s,%s,%p,%p\n",str_ptr,str,&str_ptr,&str); return 0; }

ChatGPT解説

とりあえず、仮置き。そのうち、指摘を元に修正してみようかな。

0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?