Navigation

Showing posts with label pointers. Show all posts
Showing posts with label pointers. Show all posts

Sunday, 28 October 2018

Why does C scanf only accept char a[n]?

#include <stdio.h>
int main(void)
{
    char *name;
    scanf("%s", name);
    printf("You've entered %s\n", name);

    return 0;
}
Look at the above code.
The book says it may or may not be compiled.
It can be compiled on my Linux. But it runs and I type hello, it is not right.
hello
You've entered (null)
I know name is an uninitialized pointer why may point to anything.
So the proper way to correct this is char name[81];
Now I made some change.
I initialize name at the very beginning, like this:
#include <stdio.h>
int main(void)
{
    char *name = "aaa";
    scanf("%s", name);
    printf("You've entered %s\n", name);

    return 0;
}
It can also be compiled. But when I type hello, I get this:
[Tom@localhost ~]$ ./a.out 
hello 
Segmentation fault (core dumped)
Now that name has been initialized which in my understanding should be given an specific address.
Why it still fails to work ?


Here

Sunday, 21 October 2018

Why can't pointer to float be assigned to another pointer to float in C language?

#include <stdio.h>
void print(float *p, int n); 
int main(void)
{
    float rootbeer[10];
    float things[10][5];

    things[5] = rootbeer;
    return 0;
}
Look at the above C code snippet.
rootbeer is pointer to a floating number.
things[5] is the same as things[5] + 0, which is also pointer to a floating number.
So in my view, rootbeer can be assigned to things[5].
I anticipated it should be able to be compiled successfully.
However, after gcc -std=c11 test.c
I got the error message as below:
test.c: In function ‘main’:
test.c:9:12: error: incompatible types when assigning to type ‘float[5]’ from type ‘float *’
  things[5] = rootbeer;
Why can't pointer to float be assigned to another pointer to float ?


https://stackoverflow.com/questions/3601602/what-are-rvalues-lvalues-xvalues-glvalues-and-prvalues

Is a the same as &a in scanf in C language ?

#include <stdio.h>
int main(void)
{
    float rootbeer[10];

    scanf("%f", rootbeer);

    printf("%f\n", *rootbeer);

    return 0;
}
Look at the above code.
It works well.
In my understanding, rootbeer is the same as rootbeer + 0, which is pointer to a floating number.
Now I change rootbeer in scanf to &rootbeer.
#include <stdio.h>
int main(void)
{
    float rootbeer[10];

    scanf("%f", &rootbeer);

    printf("%f\n", *rootbeer);

    return 0;
}
I anticipated an error message because I think &rootbeer is pointer to array of 10 of float.
However, the changed code can also be compiled and works perfectly.
I wonder why.
Is a the same as &a in scanf in C language ?

Here

gcc -std=c11 -Wall -Werror ***.c   should be able to show all errors, including those undefined behaviour that still can be compiled.

Wednesday, 17 October 2018

Is a[i] really the same as *(a + i) in C ?

#include <stdio.h>
int sum2d(int row, int col, int p[row][col]);
int main(void)
{
    int a[2][3] = {{1, 2, 3}, {4, 5, 6}};

    printf("%d\n", sum2d(2, 3, a));


    return 0;
}
int sum2d(int row, int col, int p[row][col])
{
    int total = 0;
    for (int i = 0; i < row; i++)
        for (int j = 0; j < col; j++)
            total += (*(p + i))[j];
    return total;
}
Look at the above code. It works perfectly.
However, after I changed p[row] into *(p + row),
#include <stdio.h>
int sum2d(int row, int col, int (*(p + row))[col]);
int main(void)
{
    int a[2][3] = {{1, 2, 3}, {4, 5, 6}};

    printf("%d\n", sum2d(2, 3, a));


    return 0;
}
int sum2d(int row, int col, int (*(p + row))[col])
{
    int total = 0;
    for (int i = 0; i < row; i++)
        for (int j = 0; j < col; j++)
            total += (*(p + i))[j];
    return total;
}
it can't be compiled and displays the following error message :
test.c:2:38: error: expected ‘)’ before ‘+’ token
 int sum2d(int row, int col, int (*(p + row))[col]);
                                      ^
test.c: In function ‘main’:
test.c:7:2: warning: implicit declaration of function ‘sum2d’ [-Wimplicit-function-declaration]
  printf("%d\n", sum2d(2, 3, a));
  ^
test.c: At top level:
test.c:12:38: error: expected ‘)’ before ‘+’ token
 int sum2d(int row, int col, int (*(p + row))[col])
At my current level, I barely understand it.
In C, I thought a[i] = *(a + i) .
Why is my code not correct ?

Here

Thursday, 11 October 2018

Why does "a const" make a C function unable to work ?

#include <stdio.h>
int sum(int n, int (*a)[n]);
int main(void)
{
    int junk[3][4] = {{2, 4, 5, 8}, {3, 5, 6, 9}, {12, 10, 8, 6}};
    int total = 0;
    for (int i = 0; i < 3; i++)
        total += sum(4, junk + i); 
    printf("%d\n", total);

    return 0;
}
int sum(int n, int (*a)[n])
{
    int subTotal = 0;
    for (int i = 0; i < n; i++)
        subTotal += (*a)[i];
    return subTotal;
}
Look at the above code which I want to calculate the sum of all elements from an array 3 of array 4 of integers.
It works nice.
Now I add a const to the function sum
#include <stdio.h>
int sum(int n, int const (*a)[n]);
int main(void)
{
    int junk[3][4] = {{2, 4, 5, 8}, {3, 5, 6, 9}, {12, 10, 8, 6}};
    int total = 0;
    for (int i = 0; i < 3; i++)
        total += sum(4, junk + i); 
    printf("%d\n", total);

    return 0;
}
int sum(int n, const int (*a)[n])
{
    int subTotal = 0;
    for (int i = 0; i < n; i++)
        subTotal += (*a)[i];
    return subTotal;
}
It can't be compiled. Here is the error message:
test.c: In function ‘main’:
test.c:8:3: warning: passing argument 2 of ‘sum’ from incompatible pointer type [enabled by default]
   total += sum(4, junk + i);
   ^
test.c:2:5: note: expected ‘const int (*)[(sizetype)(n)]’ but argument is of type ‘int (*)[4]’
 int sum(int n, const int (*a)[n]);
I thought non const should be able to be assigned to const
Where did I understand wrong?

Here might be the answer.