int n = 50;
pointer 变量占 8bytes
string s = "HI!";
#include <stdio.h>
#include <cs50.h>
int main(void)
{
string s = "HI!";
printf("%p\n", s);
printf("%p\n", &s[0]);
}
s是string "HI!"的指针;那么s[0], s[1], s[2]...组成的array s也是指针?
#include <stdio.h>
#include <cs50.h>
int main(void)
{
string s = "HI!";
printf("%p\n", s);
printf("%p\n", &s[0]);
printf("%p\n", &s[1]);
printf("%p\n", &s[2]);
printf("%p\n", &s[3]);
}
string s = "HI!";
char *s = "HI!";
string s ↔ char *s 等效
#include <stdio.h>
int main(void)
{
char *s = "HI!";
printf("%s\n", s);
}
typedef uint8_t BYTE;
typedef char *string;
#include <stdio.h>
int main(void)
{
char *s = "HI!";
printf("%c", *s);
printf("%c", *(s + 1));
printf("%c\n", *(s + 2));
}
s[1] ↔ *(s + 1) compiler translate it as
#include <stdio.h>
#include <cs50.h>
int main(void)
{
string s = get_string("s: ");
string t = get_string("t: ");
if (s == t)
{
printf("Same\n");
}
else
{
printf("Different\n");
}
}
char *s = get_string("s: ");
char *t = get_string("t: ");
string s = get_string("s: ");
string t = s;
t[0] = toupper(t[0]);
printf("%s\n", s);
printf("%s\n", t);
#include <cs50.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
char *s = get_string("s: ");
char *t = malloc(strlen(s) + 1);
for (int i = 0, n = strlen(s); i <= n; i++)
{
t[i] = s[i];
}
if (strlen(t) > 0)
{
t[0] = toupper(t[0]);
}
printf("%s\n", s);
printf("%s\n", t);
}
char *s = get_string("s: ");
char *t = malloc(strlen(s) + 1);//memory allocate
if (t == NULL)//not enough memory available
{
return 1;
}
#include <cs50.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
char *s = get_string("s: ");
if (s == NULL)
{
return 1;
}
char *t = malloc(strlen(s) + 1);//memory allocate
if (t == NULL)//not enough memory available
{
return 1;
}
strcpy(t, s);
if (strlen(t) > 0)
{
t[0] = toupper(t[0]);
}
printf("%s\n", s);
printf("%s\n", t);
free(t);//opposite of malloc,释放memory
}
NULL is the adrress zero.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int *x = malloc(3 * sizeof(int));
x[0] = 72;
x[1] = 73;
x[3] = 33;
free(x);
}
int main(void)
{
int *x;
int *y;
x = malloc(sizeof(int));
*x = 42;
*y = 13;
y = x;
*y = 13;
}
y没有assign a value, so y maybe 0, or 1000, or etc.
*y就到了某个不知道的memory的地方。
#include <stdio.h>
int main(void)
{
char s[4];
printf("s: ");
scanf("%s", s);
printf("s: %s\n", s);
}
#include <cs50.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
FILE *file = fopen("phonebook.csv", "a");
if (file == NULL)
{
return 1;
}
char *name = get_string("Name: ");
char *number = get_string("Number: ");
fprintf(file, "%s,%s\n", name, number);
fclose(file);
}
#include <stdio.h>
#include <stdint.h>
typedef uint8_t BYTE;
int main(int argc, char *argv[])
{
FILE *src = fopen(argv[1], "rb");
FILE *dst = fopen(argv[2], "wb");
BYTE b;
while(fread(&b, sizeof(b), 1, src) != 0)
{
fwrite(&b, sizeof(b), 1, dst);
}
fclose(dst);
fclose(src);
}
#include <stdio.h>
int main() {
int num = 1;
printf("%03d\n", num); // 使用%03d格式化输出,表示至少打印3位,不足的前面补零
return 0;
}
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。