完成习题11

This commit is contained in:
Wang Boyang 2025-04-06 18:36:42 +08:00
parent ec7fa6c42b
commit 1e68d514e2
4 changed files with 66 additions and 11 deletions

1
ex11/Makefile Normal file
View File

@ -0,0 +1 @@
CFLAGS=-Wall -g

BIN
ex11/ex11

Binary file not shown.

View File

@ -1,33 +1,44 @@
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
int numbers[4] = {0};
char name[4] = {'a'};
// char name[4] = {'a', 'a', 'a', 'a'};
// char name[4];
char name[4] = {255, 255, 255, 255};
char* p = numbers;
memcpy(p, &name[0], sizeof(char));
memcpy(p + 1, &name[1], sizeof(char));
memcpy(p + 2, &name[2], sizeof(char));
// first, print them out raw
printf("number: %d %d %d\n", numbers[0], numbers[1], numbers[2], numbers[3]);
printf("number: %x %d %d %d\n", numbers[0], numbers[1], numbers[2], numbers[3]);
printf("name each: %c %c %c %c\n", name[0], name[1], name[2], name[3]);
printf("name: %s\n", name);
// set up the numbers
numbers[0] = 1;
numbers[1] = 2;
numbers[2] = 3;
numbers[3] = 4;
numbers[0] = '1';
numbers[1] = '2';
numbers[2] = '3';
numbers[3] = '4';
// set up the name
name[0] = 'Z';
name[1] = 'e';
name[2] = 'd';
name[3] = '\0';
name[0] = 255;
name[1] = 255;
name[2] = 255;
name[3] = 0;
// name[3] = 'A';
// printf("%d", name);
// then print them out initialized
printf("numbers: %d %d %d %d\n", numbers[0], numbers[1], numbers[2], numbers[3]);
printf("name each: %c %c %c %c\n", name[0], name[1], name[2], name[3]);
printf("name each: %x %x %x %x\n", name[0], name[1], name[2], name[3]);
// print the name like a string
printf("name: %s\n", name);

43
ex11/ex11_copy.c Normal file
View File

@ -0,0 +1,43 @@
#include <stdio.h>
int main(int argc, char *argv[])
{
int numbers[4] = {0};
char name[4] = {'a'};
// first, print them out raw
printf("number: %d %d %d\n", numbers[0], numbers[1], numbers[2], numbers[3]);
printf("name each: %c %c %c %c\n", name[0], name[1], name[2], name[3]);
printf("name: %s\n", name);
// set up the numbers
numbers[0] = 1;
numbers[1] = 2;
numbers[2] = 3;
numbers[3] = 4;
// set up the name
name[0] = 'Z';
name[1] = 'e';
name[2] = 'd';
name[3] = '\0';
// then print them out initialized
printf("numbers: %d %d %d %d\n", numbers[0], numbers[1], numbers[2], numbers[3]);
printf("name each: %c %c %c %c\n", name[0], name[1], name[2], name[3]);
// print the name like a string
printf("name: %s\n", name);
// another way to use name
char *another = "Zed";
printf("another: %s\n", another);
printf("another each: %c %c %c %c\n", another[0], another[1], another[2], another[3]);
return 0;
}