如何在C语言中定义与使用字符串二维数组(字符三维数组)?
Hey there! Let's work through this together—your jumping jack ASCII art idea is awesome, and we'll untangle those confusing string/pointer concepts so your code clicks. First, let's fix the core issues in your code, then break down the "why" behind it all.
First, Fix the Code (And See It Work)
Your main problem is how you're defining the array of animation frames—let's simplify that, fix typos, and make the loop way easier. Here's the corrected version:
#include <stdio.h> #include <stdlib.h> // Needed for exit() #define NumOfFrames 4 // Number of animation frames #define FrameHeight 5 // Number of lines per frame // Each frame is an array of FrameHeight strings (each string = one line of ASCII art) const char *D51[NumOfFrames][FrameHeight] = { { " \\ 0 / ", " \\|/ ", " | ", " / \\ ", "_/ \\_ " }, { " ", " __0__ ", " / | \\ ", " / \\ ", " _\\ /_ " }, { " ", " o ", " /\\ /\\ ", " |/ \\| ", " _\\ /_ " }, { " ", " ", " __ ", " /_\\\\0_ ", " _\\\\/_ \\_ " } }; int main(int argc, char *argv[]) { // main should return int, not void FILE *f = fopen("log.txt","w"); if (f == NULL) { printf("Error opening file!\n"); exit(1); } // Loop through each animation frame for (int frame = 0; frame < NumOfFrames; frame++) { // Loop through each line in the current frame for (int line = 0; line < FrameHeight; line++) { // Print the entire line string directly, then add a newline fprintf(f, "%s\n", D51[frame][line]); } // Add a blank line between frames for readability fprintf(f, "\n"); } fclose(f); return 0; // main needs to return an integer value }
Now, Let's Break Down the Key Concepts You're Struggling With
Let's demystify the string/pointer/array stuff step by step:
C Strings Are Special Character Arrays: C doesn't have a built-in "string" type. Instead, a string is an array of
chars that ends with a hidden'\0'(null character). This'\0'tells functions likeprintforfprintfwhere the string ends. For example, when you write"hello", the compiler stores it as{'h','e','l','l','o','\0'}in memory.const char *= Pointer to a String: When you declareconst char *my_line, you're creating a pointer that points to the first character of a read-only string. Think of it like a signpost pointing to the start of your ASCII art line in memory—you don't need to store the entire string in the array itself, just a reference to where it lives.Your Frame Array Structure: Your animation is a hierarchy: collection of frames → each frame is a collection of lines → each line is a string. So the right array type is
const char *D51[NumOfFrames][FrameHeight]—a 2D array where each element is a pointer to a string (a line of your art). Your original 3D array was overcomplicating things because the string itself is already a character array—you don't need to specify the width manually (the'\0'takes care of that).
Why Your Original Code Wasn't Working
A few specific issues tripped you up:
- Typo:
FrameHight→ should beFrameHeight(matching your#define). - Wrong Array Type: Your 3D pointer array meant
D51[x][y][z]was a pointer, not a single character—using%cto print it tried to print a memory address as a character, which gave garbage output. - Unnecessary Loop: You don't need to loop through each character of the string—
%sinfprintfwill print the entire string until it hits the'\0'terminator.
Next Steps for Animation
Once this makes sense, moving to ncurses will be straightforward! Instead of printing to a file, you'll use mvprintw(y, x, "%s", frame_line) to draw each line of the frame at a specific position on the screen, then call refresh() to update the display. Add a small delay between frames (like napms(200) for a 200ms pause) and loop through the frames to make your jumping jack move.
内容的提问来源于stack exchange,提问作者Debian_Fanatic

