Code:
#include <stdio.h>
void test(char *str);
int main(void)
{
char a[10], b[20], c[256], d[42];
test(a);
test(b);
test(c);
test(d);
return 0;
}
void test(char *str)
{
printf("sizeof(*str) is %d\n", sizeof(str));
}
a) 8 for test(a), test(b), test(c) and test(d) as well.
b) 8 for all test calls as said above.
c) When an array is passed as argument to a function it decays to a pointer, thus the expression sizeof(str) will actually returns the size in bytes of the actual pointer in memory and not the size of the pointed array.
Code:
#include <stdio.h>
int main(void)
{
char a[10], b[20], c[256], d[42];
printf("sizeof(a) is %d\n", sizeof(a));
printf("sizeof(b) is %d\n", sizeof(b));
printf("sizeof(c) is %d\n", sizeof(c));
printf("sizeof(d) is %d\n", sizeof(d));
return 0;
}
a) 10, 20, 256 and and 42 in that order.
b) 10, 20, 256 and 42 as said above.
c) The sizeof operator determines the size in bytes of an array whose name is passed to it, as these are char type arrays each position(i.e a[0]) has exactly 1 byte size, therefore it's natural that a char [10] array will have 10 bytes of size.
Code:
#include <stdio.h>
int main(void)
{
long a[10], b[20], c[256], d[42];
printf("sizeof(a) is %d\n", sizeof(a));
printf("sizeof(b) is %d\n", sizeof(b));
printf("sizeof(c) is %d\n", sizeof(c));
printf("sizeof(d) is %d\n", sizeof(d));
return 0;
}
a) 60, 160, 2048 and 336 in that order.
b) 60, 160, 2048 and 336 as said above.
c) The same explanation of item c) from the previous example but this time the arrays are of type long which has an allocation of 8 bytes size for each position. Multiplying the dimension of the arrays by 8 gives the expected results.
Thank you for having provided the activity and for pointing out the misused sizeof operator too. I normally use it alongside with fgets to represent the input's limit in a more easier way but this time I didn’t even thought about being applying it on a pointer to an "outer" array(Yes, lack of attention is a problem for me). But a good thing is that seeing it made me also notice that the limit size of the input and arrays doesn't matter anymore in this program, the only one left is the size of the destination string for the concatenation process (MAX_CONCAT_LEN). Anyway, I was able to change the code into something more simple and clear:
EDIT: The sizes don't matter as long as they are greater than zero(by sizes I mean the arrays' dimensions in the code).
Code:
#include <stdio.h>
#include <string.h>
#define MAX_CONCAT_LEN 100
void catString(char *str, char *concat);
char *read_string(char *str, size_t i);
int main(void)
{
char string[10], catStr[MAX_CONCAT_LEN];
size_t i;
catStr[0] = '\0';
for (i = 0;read_string(string, i); i++)
catString(string, catStr);
printf("\n%s\n",catStr);
return 0;
}
void catString(char *str, char *concat)
{
char *nlPtr;
if((nlPtr = strchr(str, '\n')) != NULL) {
*nlPtr = '\0';
strcat(concat, str);
} else {
while((nlPtr = strchr(str, '\n')) == NULL) {
strcat(concat, str);
fgets(str, 10, stdin);
}
*nlPtr = '\0';
strcat(concat, str);
}
}
char *read_string(char *str, const size_t i)
{
printf("\nEnter string[%zu]= ", i);
return fgets(str, 10, stdin);
}