C言語基礎七
8638 ワード
, 。
int *p_value = NULL, *p_value1 = NULL;
p_value p_value1 ,* ,int 。NULL , 。
4
, , 。
, 。
void* , 。
, 。
1.
2.
3. ,
NULL , 0。 。
, 。 '\0' 。
, 。gcc '\0' 。 , 。 。 。
, '\0' 。
printf , %s 。 scanf , 。fgets , '
' 。
C string.h
strlen
strcat ,strncat
strcpy ,strncpy
strcmp ,strncmp
/*
*/
#include <stdio.h>
int main() {
int value = 0;
int values[] = {1, 2, 3};
printf("&value %p
", &value);
printf("values %p
", values);
printf("*values %d
", *values);
return 0;
}
/*
*/
#include <stdio.h>
int main() {
int value = 0;
int values[] = {1, 2, 3};
int *p_value = NULL, *p_value1 = NULL;
p_value = &value;
*p_value = 3;
printf("value %d
", value);
p_value = values + 2;
*p_value = 7;
printf("values[2] %d
", values[2]);
return 0;
}
/*
*/
#include <stdio.h>
int main() {
int value = 3, value1 = 7;
int value2 = 0;
int *p_value = &value, *p_value1 = &value1, *p_value2 = &value2;
/*value2 = value1;
value1 = value;
value = value2;*/
*p_value2 = *p_value1;
*p_value1 = *p_value;
*p_value = *p_value2;
printf("value %d,value1 %d
", value, value1);
return 0;
}
/*
*/
#include <stdio.h>
int main() {
int values[] = {1, 2, 3};
int *p_value = values;
char *p_char = (char*)values;
printf("sizeof(p_value) %d,sizeof(*p_value) %d
", sizeof(p_value), sizeof(*p_value));
printf("sizeof(p_char) %d,sizeof(*p_char) %d
", sizeof(p_char), sizeof(*p_char));
return 0;
}
/*
*/
#include <stdio.h>
int main() {
int values[] = {1, 2, 3, 4};
int *p_value = values, loop = 0;
for (loop = 0; loop <= 3; loop++) {
//printf("%d ", values[loop]);
//printf("%d ", p_value[loop]);
//printf("%d ", *(p_value + loop));
printf("%d ", *p_value);
p_value++;
}
printf("
");
return 0;
}
/*
*/
#include <stdio.h>
void negs(int *p_values, int num) {
int loop = 0;
for (loop = 0; loop <= (num - 1); loop++) {
//p_values[loop] = 0 - p_values[loop];
*(p_values + loop) = 0 - *(p_values + loop);
}
}
int main() {
int values[] = {4, -9, 2}, loop = 0;
negs(values, 3);
for (loop = 0; loop <= 2; loop++) {
printf("%d ", values[loop]);
}
printf("
");
return 0;
}
/*
*/
#include <stdio.h>
void swap(int *p_value, int *p_value1) {
int tmp = *p_value;
*p_value = *p_value1;
*p_value1 = tmp;
}
int main() {
int value = 3, value1 = 7;
swap(&value, &value1);
printf("value %d,value1 %d
", value, value1);
return 0;
}
/*
*/
#include <stdio.h>
void square(int *p_value) {
*p_value *= *p_value;
}
int main() {
int value = -3;
square(&value);
printf("value %d
", value);
return 0;
}
/*
*/
#include <stdio.h>
int *max(int *p_value, int *p_value1) {
if (*p_value > *p_value1) {
return p_value;
}
else {
return p_value1;
}
}
int *f() {
int value = 0;
return &value; //
}
int main() {
int value = 3, value1 = 7;
int *p_value = max(&value, &value1);
printf("*p_value %d
", *p_value);
return 0;
}
/*
void*
*/
#include <stdio.h>
int main() {
int value = 3;
char ch = 'a';
float f = 4.6f;
void *p_value = &value;
*(int*)p_value = 7;
p_value = &ch;
*(char*)p_value = 'z';
p_value = &f;
*(float*)p_value = 1.5f;
return 0;
}
/*
*/
#include <stdio.h>
int main() {
char *p_str = "abc";
char *p_str1 = "abc";
char *p_str2 = "abc""xyz";
//*p_str = 'z';
printf("*(p_str + 3) %d
", *(p_str + 3));
printf("p_str %p,p_str1 %p
", p_str, p_str1);
printf("*(p_str2 + 3) %c
", *(p_str2 + 3));
return 0;
}
/*
*/
#include <stdio.h>
int main() {
char *p_str = "abc";
char *p_str1 = "abc";
char *p_str2 = "abc""xyz";//""
//*p_str = 'z';
printf("*(p_str + 3) %d
", *(p_str + 3));//0
printf("p_str %p,p_str1 %p
", p_str, p_str1);//
printf("*(p_str2 + 3) %c
", *(p_str2 + 3));//x
return 0;
}
/*
*/
#include <stdio.h>
int main() {
//char str[] = "abc";
//char str[3] = "abc"; //‘\0’
char str[] = {'a', 'b', 'c', '\0'};
printf("sizeof(str) %d
", sizeof(str));
str[1] = 'y';
return 0;
}
/*
*/
#include <stdio.h>
int main() {
char buf[10];
//scanf("%s", buf);
fgets(buf, 10, stdin);
printf("%s
", buf);
return 0;
}
/*
*/
#include <stdio.h>
#include <string.h>
int main() {
char buf[20] = "abc";
char *p_str = NULL;
printf(" %d
", strlen("abcdef"));
p_str = strcat(buf, "xyz");
printf(" %s
", p_str);
p_str = strcpy(buf, "hello");
printf(" %s
", p_str);
printf(" %d
", strcmp("abc", "abd"));
printf(" %d
", strcmp("abd", "abc"));
printf(" %d
", strcmp("abc", "abc"));
return 0;
}
/*
*/
#include <stdio.h>
#include <string.h>
int main() {
char buf[20];
int loop = 0;
for (loop = 0; loop < 3; loop++) {
printf(" :");
fgets(buf, 20, stdin);
if (strcmp(buf, "admin
")) {
continue;
}
printf(" :");
fgets(buf, 20, stdin);
if (!strcmp(buf, "123456
")) {
break;
}
}
if (loop < 3) {
printf("
");
}
else {
printf("
");
}
return 0;
}