Study/C
[C] Memory2 ㅡ 포인터
YouYoung.
2022. 5. 25. 16:56
// Compare0.c
#include<stdio.h>
#include<cs50.h>
int main(void) {
int i = get_int("i: ");
int j = get_int("j: ");
if (i==j)
{
printf("Same\n");
}
else
{
printf("Different\n");
}
}
//Compare1.c
#include<stdio.h>
#include<cs50.h>
#include<string.h>
int main(void) {
string s = get_string("s: ");
string t = get_string("t: ");
if (strcmp(s,t)==0)
{
printf("Same\n");
}
else
{
printf("Different\n");
}
}
//Compare2.c
#include<stdio.h>
#include<cs50.h>
#include<string.h>
int main(void) {
char *s = get_string("s: ");
char *t = get_string("t: ");
if (s == t) //s와 t의 주소가 같은가?
{
printf("Same\n");
}
else
{
printf("Different\n");
}
} // 둘의 주소가 다르기 때문에 다르다고 뜬다.
//compare3.c
#include<stdio.h>
#include<cs50.h>
int main(void)
{
char *s = get_string("s: ");
char *t = get_string("t: ");
printf("%s\n",s);
printf("%s\n",t);
}
//compare4.c
#include<stdio.h>
#include<cs50.h>
int main(void)
{
char *s = get_string("s: ");
char *t = get_string("t: ");
printf("%p\n",s);
printf("%p\n",t);
}
//copy0.c
#include<cs50.h>
#include<ctype.h>
#include<stdio.h>
#include<string.h>
int main(void)
{
string s = get_string("s: ");
char *t=s;
if (strlen(t)>0) //strlen : 문자열에 담긴 문자의 갯수를 반환
{
t[0] = toupper(t[0]); // toupper 소문자를 대문자로 만들어주는 함수
}
printf("s: %s\n",s);
printf("t: %s\n",t);
}
//copy1.c
#include<cs50.h>
#include<ctype.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main(void)
{
string s = get_string("s: ");
char *t = malloc(strlen(s)+1); // 문자열을 담을 새로운 메모리 공간 확보
if (strlen(t)>0) //strlen : 문자열에 담긴 문자의 갯수를 반환
{
t[0] = toupper(t[0]); // toupper 소문자를 대문자로 만들어주는 함수
}
printf("s: %s\n",s);
printf("t: %s\n",t);
}
//copy1.c
#include<cs50.h>
#include<ctype.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main(void)
{
string s = get_string("s: ");
char *t = malloc(strlen(s)+1); // 문자열을 담을 새로운 메모리 공간 확보
for (int i=0, n=strlen(s); i<=n; i++) //strlen : 문자열에 담긴 문자의 갯수를 반환
{
t[i] = s[i]; // toupper 소문자를 대문자로 만들어주는 함수
}
t[0] = toupper(t[0]);
printf("s: %s\n",s);
printf("t: %s\n",t);
}