gpt4 book ai didi

C编程问题指针和数组2d

转载 作者:太空宇宙 更新时间:2023-11-04 03:14:33 27 4
gpt4 key购买 nike

Q2:执行以下函数 ArrPrintMatrix(char *(p)[7]) 打印 matrix[m][7] ={"SHAHBAZ","AYAZ"} 到 3x3 矩阵

示例输出

S H A                      H B A                       Z A Y ..

My question is : here is code only problem i am getting is a space after one name is completed..how to remove that space . I have this question in my assignments , that have to be submitted on sunday (11-11-18)..My code is:

#include<stdio.h>
int main()
{
void ArrPrintMatrix(char *p);//function declaration
char matrix[2][8] ={"SHAHBAZ","AYAZ"};//2d array initiliation
ArrPrintMatrix(&matrix[0][0]);//calling function with base address

}

void ArrPrintMatrix(char *p)
{
int i;
for(i=0;i<16;i++)
{
if(i>=9)//since 3 by 3 matrix is required
break;
if(i==3||i==6||i==9)//changing line since 3 by 3 matrix is needed
printf("\n");
printf("%c ",*(p+i));//prininting chracters

}
}

最佳答案

你应该使用 char (*p)[8] 而不是 char* p

以下代码可能会出错:

#include<stdio.h>
int main()
{
void ArrPrintMatrix(char (*p)[8]);//function declaration
char matrix[2][8] ={"SHAHBAZ","AYAZ"};//2d array initiliation
ArrPrintMatrix(matrix);//calling function with base address

}
void ArrPrintMatrix(char (*p)[8])
{
// i will point to one of the strings in the set of strings
// j will point into the string we are inspecting
// k will count how many characters we have printed
int i = 0, j = 0, k = 0;

// we only need to print the first 9 printable characters we find
while (k != 9)
{
// if we have reached the end of an input string (the null-terminator),
// then move on to the next element in the array, and reset
// the string pointer to the beginning of the new string
if (p[i][j] == '\0') {
++i;
j = 0;
}

// print the character we are now pointing at,
// and increment the string pointer
printf("%c ", p[i][j++]);

// keep count of how many characters we have printed
++k;

// if k is divisible by 3, start a new row
if(k%3 == 0)
printf("\n");
}
}

关于C编程问题指针和数组2d,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53229661/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com