gpt4 book ai didi

c - 使用指向另一个函数的指针传递数组

转载 作者:行者123 更新时间:2023-11-30 15:54:02 24 4
gpt4 key购买 nike

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

/* Function prototypes */
void wordLength ( char *word );
void wordConcat ( char *wordC1, char *wordC2);
void capitalizeString ( char *sentence);
void birthFormat ( char *birthday, char *bArray);


int main (void)
{

int choice;
char word [20];
char wordC1 [20];
char wordC2 [20];
char sentence [200];
char birthday [10];


char *ptr1 = "January";
char *ptr2 = "February";
char *ptr3 = "March";
char *ptr4 = "April";
char *ptr5 = "May";
char *ptr6 = "June";
char *ptr7 = "July";
char *ptr8 = "August";
char *ptr9 = "September";
char *ptr10 = "October";
char *ptr11 = "November";
char *ptr12 = "December";

char* bArray[12];

bArray[0] = ptr1;
bArray[1] = ptr2;
bArray[2] = ptr3;
bArray[3] = ptr4;
bArray[4] = ptr5;
bArray[5] = ptr6;
bArray[6] = ptr7;
bArray[7] = ptr8;
bArray[8] = ptr9;
bArray[9] = ptr10;
bArray[10] = ptr11;
bArray[11] = ptr12;







printf( "Choose a function by enterting the corresponding number: \n"
"1) Determine the length of a string\n"
"2) Concatonate 2 different words with a % between them\n"
"3) Convert a sentence to all capital letters\n"
"4) Convert birthday from MM/DD/YYYY format to Month, DD, YYYY\n"
"5) End Program\n");

scanf( "%d", &choice );
flushall();

while (choice >= 1 && choice < 5)

{

/* if statements for appropriate user prompt and calls function */
if (choice == 1)
{
/* gather user input */
printf( "\nYou have chosen to determine word length.\n"
"Please enter the word:\t");
gets( word );

/* call function to output string as well as the string length */
wordLength( word );


}

else if (choice == 2)
{
printf( "\nYou have chosen to concatenate 2 words with a % symbol in between them.\n"
"Please enter word 1:\t");

gets( wordC1 );

printf("Please enter word 2:\t");

gets( wordC2 );

/* call function to output string as well as the string length */
wordConcat( wordC1, wordC2 );
}
else if (choice == 3)
{
printf( "\nYou have chosen to convert a sentence to all capital letters.\n"
"Please enter the sentence:\t");

gets( sentence );


/* call function to output string as well as the string length */
capitalizeString ( sentence );
}
else if (choice == 4)
{
printf( "\nYou have chosen to convert your birthday from the format MM/DD/YYYY to Month, DD, YYYY\n"
"Please enter your birthday:\t");

gets( birthday );


/* call function to output string as well as the string length */
birthFormat ( birthday, bArray );
}

}
}
void wordLength( char *word )

{
int length;

printf( "\nThe string entered is: %s\n\n", word);

length = strlen (word);

printf("The string length is: %d\n", length);


}

void wordConcat(char *wordC1, char *wordC2)

{
char symbol = "%";
char result [30];
printf( "\nThe first word entered is: %s\n", wordC1);
printf( "\nThe second word entered is: %s\n", wordC2);

strcpy(result, wordC1);
strcat(result, "%");
strcat(result, wordC2);

printf("Output of both strings is: %s\n", result);


}

void capitalizeString(char *sentence)

{

int i = 0;
char letter;
printf("\nThe sentence that you have entered is: %s\n\n", sentence);

printf("The converted sentence is: \n");
while (sentence[i])
{
letter = sentence[i];
putchar (toupper(letter));
i++;
}
}

void birthFormat(char birthday, char* bArray)
{
printf("\n [%s]\n", bArray[1]);
}

我已经尝试编写这个程序超过 8 个小时了,现在正在写最后一部分,只是无法弄清楚最后一点!

由于某种原因,每当我将数组从主函数传递到函数birthFormat时,它都不会通过。传过去后数组为空! (我认为)

最佳答案

当你传入它时,bArray 不是一个 char* bararray(一个字符数组),它是一个 char 指针数组。所以你可以做 char**

关于c - 使用指向另一个函数的指针传递数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13338135/

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