a3q1.c: In function ‘main’:a3q1.c:21:12: warning: passing argument 1 of ‘printf’ from incompatible pointer type [-Wincompatible-pointer-types] 21 | printf(arr,n,k); | ^~~ | | | int *In file included from a3q1.c:1:/usr/include/stdio.h:332:43: note: expected ‘const char * restrict’ but argument is of type ‘int *’ 332 | extern int printf (const char *__restrict __format, ...); | ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~
Question
a3q1.c: In function ‘main’:a3q1.c:21:12: warning: passing argument 1 of ‘printf’ from incompatible pointer type [-Wincompatible-pointer-types] 21 | printf(arr,n,k); | ^~~ | | | int *In file included from a3q1.c:1:/usr/include/stdio.h:332:43: note: expected ‘const char * restrict’ but argument is of type ‘int *’ 332 | extern int printf (const char *__restrict __format, ...); | ~~~~~~~~~~~~~~~~~~^
Solution
The error message you're seeing is because you're trying to pass an integer pointer (int *) to the printf function, which expects a string (const char * restrict) as its first argument.
The printf function in C uses the first argument as a format string, which tells it how to format the remaining arguments.
Here's how you can fix this:
printf("%d %d", n, k);
In this case, "%d %d" is the format string. The %d is a placeholder that gets replaced with an integer. So this will print the values of n and k as integers.
If arr is an array of integers and you want to print its elements, you can use a loop:
for(int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
Replace size with the actual size of your array. This will print each element of the array followed by a space.
Similar Questions
What is the following is invalid header file in C?*
Which of the following is an invalid identifier in C?
Write a C program that prints exactly and that piece of art is useful" - Dora Korpar, 2015-10-19, followed by a new line, to the standard error.You are not allowed to use any functions listed in the NAME section of the man (3) printf or man (3) putsYour program should return 1Your program should compile without any warnings when using the -Wall gcc option
What will the following code output?#include <stdio.h>int main() { int arr[3] = {1, 2, 3}; int *p = arr; printf("%d\n", *(p + 2)); return 0;}
Point out the error line in the following program.1. #include<stdio.h>2. struct emp3. {4. char name[20];5. int age;6. };7. int main()8. {9. emp int xx;10. int a;11. printf("%d\n", a);12. return 0;13. }Select one:Line number 7Line number 9Line number 4None of these.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.