invalid operands to binary - (have 'int *' and 'int **') - c

#include<stdio.h>
void main()
{
int a[]= {10,20,30,40,50,60};
int *p[]= {a,a+1,a+2,a+3,a+4,a+5};
int **pp=p;
pp++;
printf("%d,%d,%d", pp-p,*pp-a, **pp);
*pp++;
printf("%d,%d,%d", pp-p, *pp-p, *pp-p);
++*pp;
printf("%d,%d,%d", pp-p, *pp-a, **pp);
++**pp;
printf("%d,%d,%d", pp-p, *pp-a, **pp);
}
it's showing this error:
invalid operands to binary - (have 'int *' and 'int **')
printf("%d,%d,%d", pp-p, *pp-p, *pp-p);
invalid operands to binary - (have 'int *' and 'int **')
printf("%d,%d,%d", pp-p, *pp-p, *pp-p);

*pp-p, you cannot subtract a int** from an int* since they are not compatible types. It should be pp-p.
Also please note that in order to print addresses with printf, you should cast the pointers to void* and print using %p.

Related

My compiler returned "assignment to 'int *' from incompatible pointer type 'char *' [-Wincompatible-pointer-types]" when I ran my C program

#include <stdio.h>
int main()
{
char grade = 'A';
int *p = &grade;
printf("The address where the grade is stored: %p\n", p);
printf("Grade: %c\n", grade);
return 0;
}
I get this error each time I compile the code on VScode but never on code blocks.
warning: incompatible pointer types initializing 'int *' with an
expression of type 'char *' [-Wincompatible-pointer-types]
int *p = &grade;
^ ~~~~~~
1 warning generated.
 ./main
The address where the grade is stored: 0x7ffc3bb0ee2b
Grade: A"
The warning tells you exactly what it is. It says:
incompatible pointer types initializing 'int *' with an expression of type 'char *'
This means that p is of type int *, that &grade is of type char *, and that these two pointers are not compatible. The solution is to change the declaration of p to:
char *p = &grade;
One more thing. Usually you can safely do implicit conversions to and from any pointer to void *, but not when passed as argument to a variadic function. If you want to print the address, use this:
printf("%p", (void *)p);
But only cast when needed. Never do it just to get rid of warnings. Here is an answer I wrote about that: https://stackoverflow.com/a/62563330/6699433
As an alternative, you could use a void pointer directly:
void *p = &grade;
printf("%p", p);
But then you would need to cast if you want to dereference it. For example:
char c = *(char*)p;
That cast is not necessary if p is declared as char *.

What is wrong with this c code with a pointer to struct with pointer?

Can someone please explain what is wrong with this code? And why I'm getting the compilation error? And how to solve this issue?
struct recPtr_t{
int * l;
int * w;
};
typedef struct recPtr_t recPtr;
recPtr r1 = {0,0};
recPtr * rPtr1 = &r1;
printf("Default dimensions of rectangle are %d and %d\n", rPtr1->l, rPtr1->w);
The warning:
rectangle.c:28:59: warning: format specifies type 'int' but the argument has type 'int *' [-Wformat]
printf("Default dimensions of rectangle are %d and %d\n", (int *)rPtr1->l, (int *)rPtr1->w);
~~ ^~~~~~~~~~~~~~~
rectangle.c:28:76: warning: format specifies type 'int' but the argument has type 'int *' [-Wformat]
printf("Default dimensions of rectangle are %d and %d\n", (int *)rPtr1->l, (int *)rPtr1->w);
~~ ^~~~~~~~~~~~~~~
You define the struct to hold (int *) but when initializing the struct you pass in integers. That’s the problem. It’s just a warning so the compiler is letting you know what you have done. It will take the 0 to be the value of the (int *) which will cause problems later on.
you should change your struct to contain integers instead of integer pointers.
you should have:
struct recPtr_t{
int l;
int w;
};
this will solve your issue.

note: expected 'float *' but argument is of type 'int *'

I have a warning
note: expected 'float *' but argument is of type 'int *'
My program is
#include <stdio.h> //header file
void foo(float *);
int main()
{
int i = 10, *p = &i;
foo(&i);
}
void foo(float *p)
{
printf("%f\n", *p);
}
Output is 0.000 000
Why?
The foo function expects a parameter of type int * (pointer to integer), not of type float * (pointer to float). Change the data type float to an int and substitute "%d" instead of "%f" to get rid of the warning.
As warning says, you're passing to foo() a pointer to int, but foo() expects a pointer to float. So, or you declare i as float, or change the parameter of foo() as int*.

Trouble Understanding C Basics

I am attempting to get into C programming, and I am having problems with assigning and pulling data from C-arrays, (in this case, to and from C-style strings).
Please point out any faults you see here.
I am primarily a c++/python programmer, so please keep the explanations of memory usage and management as simple as possible.
#include <stdio.h>
typedef struct AuthorInfo {
char* firstName;
char* lastName;
} AuthorInfo;
typedef struct BookEntry {
char bookID;
char* bookName;
AuthorInfo author;
} BookEntry;
void assign_str(const char** from, char** to) {
int size = sizeof(from)/sizeof(char);
printf((char)size);
printf('\n');
for (int i=0; i < size; i++) {
(*to)[i] = (*from)[i];
};
};
BookEntry BookEntry_(const int id, const char* bName, const char* aF, const char*aL, BookEntry* ret) {
ret->bookID = id;
ret->bookName = (char*)malloc(sizeof(bName));
ret->author.firstName = (char*)malloc(sizeof(aF));
ret->author.lastName = (char*)malloc(sizeof(aL));
assign_str(bName, &ret->bookName);
assign_str(aF, &ret->author.firstName);
assign_str(aL, &ret->author.lastName);
}
void display_book(BookEntry* entry) {
printf(entry->bookName);
printf('\n');
printf(entry->author.firstName);
printf(' ');
printf(entry->author.lastName);
printf('\n');
};
int main(int argc, char** args) {
BookEntry book;
book.bookID = 0;
assign_str("Tom Sawyer", &book.bookName);
assign_str("Mark", &book.author.firstName);
assign_str("Twain", &book.author.lastName);
display_book(&book);
return 0;
};
Compiling this code with gcc goof.c -o goof -std=c11 results in :
goof.c: In function ‘assign_str’:
goof.c:16:10: warning: passing argument 1 of ‘printf’ makes pointer from integer without a cast [-Wint-conversion]
printf((char)size);
^
In file included from goof.c:1:0:
/usr/include/stdio.h:362:12: note: expected ‘const char * restrict’ but argument is of type ‘char’
extern int printf (const char *__restrict __format, ...);
^
goof.c:16:3: warning: format not a string literal and no format arguments [-Wformat-security]
printf((char)size);
^
goof.c:17:10: warning: passing argument 1 of ‘printf’ makes pointer from integer without a cast [-Wint-conversion]
printf('\n');
^
In file included from goof.c:1:0:
/usr/include/stdio.h:362:12: note: expected ‘const char * restrict’ but argument is of type ‘int’
extern int printf (const char *__restrict __format, ...);
^
goof.c:17:3: warning: format not a string literal and no format arguments [-Wformat-security]
printf('\n');
^
goof.c: In function ‘BookEntry_’:
goof.c:25:26: warning: implicit declaration of function ‘malloc’ [-Wimplicit-function-declaration]
ret->bookName = (char*)malloc(sizeof(bName));
^
goof.c:25:26: warning: incompatible implicit declaration of built-in function ‘malloc’
goof.c:25:26: note: include ‘<stdlib.h>’ or provide a declaration of ‘malloc’
goof.c:28:14: warning: passing argument 1 of ‘assign_str’ from incompatible pointer type [-Wincompatible-pointer-types]
assign_str(bName, &ret->bookName);
^
goof.c:14:6: note: expected ‘const char **’ but argument is of type ‘const char *’
void assign_str(const char** from, char** to) {
^
goof.c:29:14: warning: passing argument 1 of ‘assign_str’ from incompatible pointer type [-Wincompatible-pointer-types]
assign_str(aF, &ret->author.firstName);
^
goof.c:14:6: note: expected ‘const char **’ but argument is of type ‘const char *’
void assign_str(const char** from, char** to) {
^
goof.c:30:14: warning: passing argument 1 of ‘assign_str’ from incompatible pointer type [-Wincompatible-pointer-types]
assign_str(aL, &ret->author.lastName);
^
goof.c:14:6: note: expected ‘const char **’ but argument is of type ‘const char *’
void assign_str(const char** from, char** to) {
^
goof.c: In function ‘display_book’:
goof.c:34:3: warning: format not a string literal and no format arguments [-Wformat-security]
printf(entry->bookName);
^
goof.c:35:10: warning: passing argument 1 of ‘printf’ makes pointer from integer without a cast [-Wint-conversion]
printf('\n');
^
In file included from goof.c:1:0:
/usr/include/stdio.h:362:12: note: expected ‘const char * restrict’ but argument is of type ‘int’
extern int printf (const char *__restrict __format, ...);
^
goof.c:35:3: warning: format not a string literal and no format arguments [-Wformat-security]
printf('\n');
^
goof.c:36:3: warning: format not a string literal and no format arguments [-Wformat-security]
printf(entry->author.firstName);
^
goof.c:37:10: warning: passing argument 1 of ‘printf’ makes pointer from integer without a cast [-Wint-conversion]
printf(' ');
^
In file included from goof.c:1:0:
/usr/include/stdio.h:362:12: note: expected ‘const char * restrict’ but argument is of type ‘int’
extern int printf (const char *__restrict __format, ...);
^
goof.c:37:3: warning: format not a string literal and no format arguments [-Wformat-security]
printf(' ');
^
goof.c:38:3: warning: format not a string literal and no format arguments [-Wformat-security]
printf(entry->author.lastName);
^
goof.c:39:10: warning: passing argument 1 of ‘printf’ makes pointer from integer without a cast [-Wint-conversion]
printf('\n');
^
In file included from goof.c:1:0:
/usr/include/stdio.h:362:12: note: expected ‘const char * restrict’ but argument is of type ‘int’
extern int printf (const char *__restrict __format, ...);
^
goof.c:39:3: warning: format not a string literal and no format arguments [-Wformat-security]
printf('\n');
^
goof.c: In function ‘main’:
goof.c:45:14: warning: passing argument 1 of ‘assign_str’ from incompatible pointer type [-Wincompatible-pointer-types]
assign_str("Tom Sawyer", &book.bookName);
^
goof.c:14:6: note: expected ‘const char **’ but argument is of type ‘char *’
void assign_str(const char** from, char** to) {
^
goof.c:46:14: warning: passing argument 1 of ‘assign_str’ from incompatible pointer type [-Wincompatible-pointer-types]
assign_str("Mark", &book.author.firstName);
^
goof.c:14:6: note: expected ‘const char **’ but argument is of type ‘char *’
void assign_str(const char** from, char** to) {
^
goof.c:47:14: warning: passing argument 1 of ‘assign_str’ from incompatible pointer type [-Wincompatible-pointer-types]
assign_str("Twain", &book.author.lastName);
^
goof.c:14:6: note: expected ‘const char **’ but argument is of type ‘char *’
void assign_str(const char** from, char** to) {
^
And running the code causes bash to say:
Segmentation fault (core dumped)
There's many errors in there and no array, only structs.
first, you must include the stdlib library (#include <stdlib.h>)
secondly, the printf function can't be used like that.
This function need a string to know how to print the data ex:printf("an int: %d",myInt); or printf("a string: %s",myString);. Note the %d or %s they indicate where to put the data.
thirdly I think you want this void assign_str(const char* from, char** to)

How can I remove format warning in c language?

#include <stdio.h>
int main(void) {
int *pVar, var = 10;
pVar = &var;
//*pVar = var;
printf("value = %d, address = Ox%X\n", var, &var);
// Format specifies type 'unsigned int' but the argument has type 'int *'
printf("pValue = %d, address = Ox%X\n", *pVar, pVar);
// Format specifies type 'unsigned int' but the argument has type 'int *'
*pVar = 20;
printf("value = %d, address = Ox%X\n", var, &var);
// Format specifies type 'unsigned int' but the argument has type 'int *'
printf("pValue = %d, address = Ox%X\n", *pVar, pVar);
// Format specifies type 'unsigned int' but the argument has type 'int *'
return 0;
}
I find some warning
Format specifies type unsigned int but the argument has type int *
Even though the program runs as I intend, I want to know why this happen.
What should I do to remove those errors without making some errors in result?
You can use %p format for pointers as in
printf("value = %d, address = %p\n", var, (void *) &var);

Resources