#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void makeArray3(void); /* ??????? ?????? ?? 3? ???????? ? ??????? ???????? ?? ????? */
void usingPointers(void); /* ??????? ?????? ?? 3? ???????? ? ??????? ???????? ?? ?????, ????????? ????????? */
void useSwap(void); /* ?????? ??????? ???????? ???? ?????????? */
void useSlength(void); /* ????????? ????? ?????? */
char *getstr(); /* ???? ?????? */
int main()
{
makeArray3();
usingPointers();
useSlength();
useSwap();
return 0;
}
void makeArray3(void)
{
int a[3];
int i;
printf("Function makes an array with 3 elements\nInput elements: ");
for(i = 0; i < 3; ++i)
scanf("%d%*c", &a[i]);
for(i = 0; i < 3; ++i)
printf("a[%d] = %d\t", i
, a
[i
]);
}
void usingPointers(void)
{
int a[3];
int *p=a, i;
printf("\n\nSame function, using pointers\nInput elements: ");
for(i = 0; i < 3; ++i)
scanf("%d%*c", &*(p+i));
for(i = 0; i < 3; ++i)
printf("p[%d] = %d\t", i
, *(p
+i
));
free(p);
}
void intSwap(int *a, int *b) /* ?????? ??????? ???????? ???? ?????????? */
{
int t = *a;
*a = *b;
*b = t;
}
void useSwap(void)
{
int x, y;
printf("\n\nFunction swaps x and y\nInput x, y: ");
scanf("%d%*c%d", &x, &y);
intSwap(&x, &y);
printf("Result : x = %d\ty = %d\n", x
, y
);
}
int slength(char *ptr) /* ????????? ????? ?????? */
{
char *p = ptr;
for(; *p; ++p)
;
return p - ptr;
}
void useSlength(void)
{
char buf[81];
int l;
printf("\n\nFunction counts chars in a strig\nInput string...\n");
l = scanf("%80[^\n]", buf); // l ????? ???????, ???? ??????? ???????, ???? ??? - ????
if(l == 0)
{ // [^\n] ????????, ??? ?????? ????? ??????????? ?? ??????? \n
*buf = '\0';
scanf("%*c");
}
l = slength(buf);
printf("string \"%s\" has %d chars", buf
, l
);
}
char *getstr() /* ???? ?????? */
{
char *ptr = (char *)malloc(1);
char buf[81];
int n, len = 0;
*ptr = '\0';
do
{
n = scanf("%80[^\n]", buf);
if(n < 0)
{
free(ptr);
ptr = NULL;
continue;
}
if(n == 0)
scanf("%*c");
else
{ // strlen - ??????? ??? ???????? ????? ????-??????????????? ??????
len += strlen(buf); // ??? ??????? ????????? ?????? (string.h)
ptr = (char *) realloc(ptr, len + 1);
strcat(ptr, buf); // char *strcat(char *str1, const char *str2); (string.h)
} // strcat ???????????? ? ?????? str1 ????? ?????? str2 ? ?????????
} // ?????? str1 ??????? ????????, ?????????? ???????? ????????? str1
while(n > 0);
return ptr;
}