简体   繁体   English

为什么打印二维字符数组会产生垃圾值?

[英]Why does printing a 2D array of characters give garbage values?

I am implementing a function that prints a 2D array of characters only using a double-pointer and pointer notation.我正在实现一个 function ,它只使用双指针和指针表示法打印一个二维字符数组。 When I run the code, it prints a bunch of garbage values in the format I want instead of the correct characters.当我运行代码时,它会以我想要的格式而不是正确的字符打印一堆垃圾值。

My professor instructed me not to use arr[row][col], instead, I must access it using ( (arr+i)+j) or similar我的教授指示我不要使用 arr[row][col],而是必须使用( (arr+i)+j) 或类似方法访问它

This is a project for a class and I can't change any of the code outside of this function.这是一个 class 的项目,我无法更改此 function 之外的任何代码。 The characters are meant to be formatted like a word search puzzle.这些字符的格式类似于单词搜索谜题。 The arguments passed to my function are char** arr, int size .传递给我的 function 的 arguments 是char** arr, int size

This is my function:这是我的 function:

for(int i = 0; i < size; i++){
    printf("%c %c %c %c %c %c %c %c %c %c %c %c %c %c %c\n", *(arr+i), *(arr+i)+1, *(arr+i)+2, *(arr+i)+3, *(arr+i)+4, *(arr+i)+5, *(arr+i)+6, * 
(arr+i)+7, *(arr+i)+8, *(arr+i)+9, *(arr+i)+10, *(arr+i)+11, *(arr+i)+12, *(arr+i)+13, *(arr+i)+14 );   }

Expected output:预期 output:

W D B M J Q D B C J N Q P T I
I R Z U X U Z E A O I O R T N
M N Z P L R N H L Y L X H M D
M Y E K A I D P I U L Y O W I
A O A B A R K U F V I H L A A
L O N M R X K I O J N A V R N
A E P T A A R A R T O W A I A
S U C Z A U S I N A I A L Z V
K O T A O N R K I S S I A O N
A H X S V K A I A E A I B N E
U D S X N X C C D W G S A A V
O I S D W L E J N J T X M H A
M O X W T N H Q D X O Q A Q D
R U U V G E O R G I A Q V D A
V F L O R I D A L G L W O X N

Actual output:实际 output:

░ ▒ ▓ │ ┤ ╡ ╢ ╖ ╕ ╣ ║ ╗ ╝ ╜ ╛
≡ ± ≥ ≤ ⌠ ⌡ ÷ ≈ ° ∙ · √ ⁿ ² ■

0 1 2 3 4 5 6 7 8 9 : ; < = >
P Q R S T U V W X Y Z [ \ ] ^
p q r s t u v w x y z { | } ~
É æ Æ ô ö ò û ù ÿ Ö Ü ¢ £ ¥ ₧
░ ▒ ▓ │ ┤ ╡ ╢ ╖ ╕ ╣ ║ ╗ ╝ ╜ ╛
╨ ╤ ╥ ╙ ╘ ╒ ╓ ╫ ╪ ┘ ┌ █ ▄ ▌ ▐
≡ ± ≥ ≤ ⌠ ⌡ ÷ ≈ ° ∙ · √ ⁿ ² ■

0 1 2 3 4 5 6 7 8 9 : ; < = >
P Q R S T U V W X Y Z [ \ ] ^
└ ┴ ┬ ├ ─ ┼ ╞ ╟ ╚ ╔ ╩ ╦ ╠ ═ ╬
` a b c d e f g h i j k l m n

If arr is really a char** , then you need to dereference twice to get a char .如果arr真的是char** ,那么您需要取消引用两次才能获得char

So, in your statement, arr+i is another char** , pointing at a char* i steps further along from the one arr points at.因此,在您的陈述中, arr+i是另一个char** ,指向一个char* i从一个arr指向的位置更远。 Hopefully arr points at the beginning of an array of char* at least size long.希望arr指向至少size长的char*数组的开头。

Now *(arr+i) dereferences it, fetching the char* pointed to by arr+i , giving you a char* .现在*(arr+i)取消引用它,获取arr+i指向的char* ,给你一个char*

Now *(arr+i)+7 , for example, is another char* , pointing at a char 7 steps further along from the one *(arr+i) points at.例如,现在*(arr+i)+7是另一个char* ,指向距离*(arr+i)点更远的char 7 步。 Hopefully *(arr+i) points at the beginning of an array of char at least 15 long.希望*(arr+i)指向至少 15 长的char数组的开头。

But you don't dereference it, so you're attempting to print the value of the pointer (ie the address it holds), not the value it points to (the char ).但是你没有取消引用它,所以你试图打印指针的值(即它持有的地址),而不是它指向的值( char )。

Try *(*(arr+i)+7) .试试*(*(arr+i)+7)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM