-1

我正在尝试在 C 中填充一个二维数组。一切正常,但分配给数组的值没有打印出来。

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h> 
#include <string.h> 

main()
{
    char parkingspace[25][4];
    char CarReg[7], validreg[7];
    int row, position;

    printf( "Enter the car Registration number \n" );
    fgets( CarReg, sizeof( CarReg ), stdin );
    if( isdigit( CarReg[0] )&& isdigit( CarReg[1] ) && (CarReg[2]=='H' ) && ( CarReg[3]=='I' ) && ( CarReg[4]=='R' ) && ( CarReg[5]=='E' ))
    {
        puts( "Valid Registration \n" );
        printf( "==================================================\n\n\n" );
    }
    else
    {   
        puts( "Invalid registration .\n Please put a value of two digits followed by the word HIRE! in caps" );
    }
    printf( "You entered: %s\n", CarReg );
    if( isdigit( CarReg[0] )&& isdigit( CarReg[1] ) && ( CarReg[2]=='H' ) && (CarReg[3]=='I' ) && ( CarReg[4]=='R' ) && ( CarReg[5]=='E' ))
    {
        strcpy(validreg, CarReg);
        printf( "Accepted Car Reg is : %s\n\n\n\n", validreg );
        printf( "==================================================\n\n\n");
    }
    for (row=1; row<26; row++)
    {
        for (position=1;position<5; position++)
        {
            parkingspace[row][position]=validreg;
            printf("parkingspace \t row[%d] position[%d] =[ %c ]\n", row,position,parkingspace[row][position]);
        }
    }   
}
4

3 回答 3

1

您遇到的一个问题是数组的索引从零到数组大小减一。所以有效的索引row0to ,而24不是像你一样。125


另一个问题是您尝试将字符数组分配给单个字符:

parkingspace[row][position]=validreg;

如果您只想要 in 的前四个字符,validregparkingspace[row]对内部循环执行以下操作:

for (position=0; position < 4; position++)
{
    parkingspace[row][position] = validreg[position];
    printf("parkingspace \t row[%d] position[%d] =[ %c ]\n", row, position, parkingspace[row][position]);
}
于 2013-03-06T11:43:26.487 回答
0

你在第 39 行犯了一个错误

parkingspace[row][position]=validreg;

应该

parkingspace[row][position]=validreg[position];
于 2013-03-06T11:42:17.747 回答
0
for (row=1; row<26; row++)
{
    for (position=1;position<5; position++)

这两行应该修改为

for (row = 0; row < 24; row ++)
{
   for (position =0; position < 3; poisition++)

这是因为您已声明为 char 停车位[25][4];数组从索引 '0' 开始直到 (size - 1)

于 2013-03-06T12:24:07.643 回答