Stick to the original title ：

Write a program that outputs the string representation of numbers from 1 to n.

But for multiples of three it should output “Fizz” instead of the
number and for the multiples of five output “Buzz”. For numbers which
are multiples of both three and five output “FizzBuzz”.

Example:

n = 15,

Return: [
“1”,
“2”,
“Fizz”,
“4”,
“Buzz”,
“Fizz”,
“7”,
“8”,
“Fizz”,
“Buzz”,
“11”,
“Fizz”,
“13”,
“14”,
“FizzBuzz” ]

code :
/** * Return an array of size *returnSize. * Note: The returned array must be
malloced, assume caller calls free(). */ char** fizzBuzz(int n, int*
returnSize) { *returnSize=n;char buff[10];
// For storage "Fizz","Buzz","FizzBuzz" as well as 1246 What ; Length should be greater than "FizzBuzz" Length of +1 char **s=(char **)
malloc(n*sizeof(char *));// Array pointer for(int i=1; i<=n; i++) { if(i%15==0)//15 Multiple of {
sprintf(buff, "%s", "FizzBuzz");// hold "FizzBuzz" existence buff } else if(i%3==0)//3 Multiple of {
sprintf(buff, "%s", "Fizz"); } else if(i%5==0)//5 Multiple of { sprintf(buff, "%s",
"Buzz"); } else// other { sprintf(buff, "%d", i); } *(s+i-1)=malloc(sizeof(buff));
// by s The space needed for this bit allocation of memcpy(*(s+i-1), buff, strlen(buff)+1);// hold buff Copy stored data to s array
memset(buff, ' ', 11);// hold buff empty , Convenient for next use } return s; }
note ：（1,2 From Baidu Encyclopedia ）
1. sprintf function ： Write formatted data to a string ;
2. void *memcpy(void *dest, const void *src, size_t n);
function ： From source src Start copying at the beginning of the indicated memory address n Bytes to destination dest At the beginning of the memory address .
4. It's important to note that ：1, The return value type is char** and char* Of difference .2, The difference between pointer array and array pointer .
5. What I understand now is ,char** The return value type of is an array of pointers , and char* Is an array of values of type .
6. The difference between a pointer array and an array pointer is probably , Pointer array is an array in which all elements are pointers , An array pointer is a pointer to an array .

Technology
Daily Recommendation