Invalid operands to binary * ( have 'char *' and 'char **' ) error - c

I have write simple program as follows.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
char* alphabate[]={
(char *)"xyz",
(char *)"abc",
(char *)"pqr",
NULL
};
void main()
{
char **pp;
for( pp=alphabate; *pp; pp++ )
{
printf("\n alphabate member %s" *pp);
}
}
but when i compile it on my Linux machine then it show following error at printf() statement.
test.c:19: error: Invalid operands to binary * ( have 'char *' and 'char **' )
Any idea to resolve it?

printf("\n alphabate member %s" *pp);
you are missing a comma before *pp

printf("\n alphabate member %s" *pp);
should be
printf("\n alphabate member %s", *pp);

Use a , in printf after the printf("\n alphabate member %s",*pp);
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
char* alphabate[]={
(char *)"xyz",
(char *)"abc",
(char *)"pqr",
NULL
};
int main()
{
char **pp;
for( pp=alphabate; *pp; pp++ )
{
printf("\n alphabate member %s",*pp);
}
return 0;
}

There's a comma missing after the string in printf. It must be
printf("\n alphabate member %s", *pp);
With your code, the * is interpreted as multiplication.

Casting string literals to char * is redundant. Also the signature of main in C should be either:
int main(void)
or
int main(int argc, char *argv[])
You are missing a comma in the printf function. It should be
printf("\n alphabate member %s", *pp);

Related

how put string in a list with struct ? (C language)

main.c:
#include "lista.h"
#include <stdio.h>
int main(void){
LISTA p1;
iniciaLista(&p1);
inserir(&p1, "Fabio");
return 0;
}
lista.h:
#include <stdio.h>
#include <stdbool.h>
#define MAX 60
typedef struct{
char nome[50];
}REGISTRO;
typedef struct{
REGISTRO A[MAX +1];
int qtdElemen;
}LISTA;
void iniciaLista(LISTA* l);
void inserir(LISTA* l, char ch);
lista.c:
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include "lista.h"
void iniciaLista(LISTA* l){
l->qtdElemen = 0;
}
void inserir(LISTA* l, char ch){
int i = l->qtdElemen;
if(l->qtdElemen == MAX) return;
else{
strcpy(&ch, (l->A[i].nome);
l->qtdElemen++;
}
return;
}
i'm trying put a name in a list but i cant and dont know why, What can I be doing wrong?, I get several errors when I try to run:
lista.c:26:32: warning: passing argument 2 of ‘strcpy’ makes pointer from integer without a cast [-Wint-conversion]
In file included from lista.c:3:
/usr/include/string.h:125:70: note: expected ‘const char * restrict’ but argument is of type ‘char’
void inserir(LISTA* l, const char ch);
ch is a single character and not a string (char[] / char*)
you also should make sure that ch is really null terminated and with less than 50 characters ortherwise if it's the user that inputs data he can perform a bufferoverflow attack. i'd rather use strncpy/strlcpy here
There are two distinct problems:
in main is should be inserir(&p1, "Fabio"); instead of void inserir(&p1, "Fabio");
in the declaration and the implementation of inserir it should be void inserir(LISTA* l, const char *ch) (emphasis on const char *ch).

expected ‘char ** restrict’ but argument is of type ‘char (*)[x]’

I've never understood this error and I keep running into similar one's and it's really frustrating as I can't find a solution to it. (If there is another one please don't bash on me as I couldn't find it).
Looking at a simple piece of code:
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char *argv[]){
char s[10];
int x = strtol(argv[1], &s, 10);
printf("%d", x);
printf("%s", s);
return 0;
}
I keep receiving these errors, but I don't really understand why:
warning: passing argument 2 of ‘strtol’ from incompatible pointer type [-Wincompatible-pointer-types]
int x = strtol(argv[1], &s, 10);
and
note: expected ‘char ** restrict’ but argument is of type ‘char (*)[10]’
When I change char s[10] to char *s I receive a segfault on the line that uses strtol. I don't understand what's going wrong, could someone explain? Thanks in advance.
Changing char s[10] to char *s is the correct way to address the compile error. strtol's second argument is supposed to be a pointer to a pointer variable, which it will initialize to point somewhere within the string that is its first argument. char s[10] does not declare a pointer variable, char *s does.
The only explanation I can think of, why this program might crash, is that you didn't pass it any arguments. In that case argc will be less than 2 and argv[1] will be a null pointer (or possibly not even initialized). You need to do something like this:
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char *argv[])
{
if (argc != 2) {
fprintf(stderr, "usage: %s integer\n", argv[0]);
return 1;
}
char *s;
long x = strtol(argv[1], &s, 10);
printf("x: %ld\n", x);
printf("s: '%s'\n", s);
return 0;
}
Incidentally, in C, for historical reasons, preferred style is to put the opening curly brace of a function definition on its own line, even if all other opening curly braces are "cuddled".
&s is referring to the address of the array. It is effectively a pointer to a pointer. What you want is to pass in the array name s which is a pointer pointing to the first character of the array s.
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char *argv[]){
char s[10];
int x = strtol(argv[1], s, 10);
printf("%d", x);
printf("%s", s);
return 0;
}
You need to create a variable of char * and then pass this to function.
It is not possible to pass array that way as it will not decay to char **.
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char *argv[]){
char s[10];
char* s_ptr; //New variable, char *
int x = strtol(argv[1], &s_ptr, 10);
printf("%d", x);
printf("%s", s_ptr);
return 0;
}
See documentation of strtol function:
http://www.cplusplus.com/reference/cstdlib/strtol/

How to convert properly char * const[256] to const char * const*

I have this struct:
typedef struct cmdLine {
char * const arguments[256];
} cmdLine;
I also have an argument cmdLine *pCmdLine. I want to use execvso I write execv((pCmdLine->arguments[0]), pCmdLine->arguments);. The second argument doesn't feet properly to execvand I want to ask how to convert it properly.
The warning I get is: Passing 'char* const[256]'' to parameter of type 'const char *const *' discards qualifiers in nested pointer types. I would lie for some help to convert it properly, thanks.
I do not see any problems:
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdio.h>
struct
{
char * const arr[20];
}*str;
void foo(char *const par[])
{
volatile const char * const ptr = par[4];
printf("%s\n", par[7]);
printf("%s\n", ptr);
}
void foo1()
{
foo(str -> arr);
}
https://godbolt.org/z/4Sv4a8

C - issue with reading in from command line - assignment makes pointer from integer without a cast

Having an issue with reading in from the command line into an array of integers.
Intent is to parse the entered line by spaces/tabs and then atoi() each individual number into the appropriate array slot.
Relevant code:
main.c
#include <stdio.h>
#include <stdlib.h>
#include "functions.h"
int main()
{
int nums[100];
int count = 0;
readInput(&nums, &count);
return 0;
}
functions.h
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef FUNCTIONS_H_INCLUDED
#define FUNCTIONS_H_INCLUDED
void readInput(int *nums[], int *count);
#endif // FUNCTIONS_H_INCLUDED
functions.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "functions.h"
#define delims " \t\r\n"
#define MAX_LEN 128
void readInput(int *nums[], int *count)
{
char *input = malloc(MAX_LEN);
char *buffer;
gets(input);
buffer = strtok(input, delims);
nums[(*count)++] = atoi(buffer);
while ((buffer = strtok(NULL, delims)) != NULL)
nums[(*count)++] = atoi(buffer);
buffer = strtok(NULL, delims);
}
The lines in functions.c with nums[(*count)++] = atoi(buffer); are flagging with the warning "warning: assignment makes pointer from integer without a cast".
And from main.c, the line readInput(&nums, &count); is flagging with "warning: passing argument 1 of 'readInput' from incompatible pointer type".
The odd thing is this program works when run and any attempts to add in casting or dereferencing has resulted in the warnings being subdued, but the program crashing when run.
When you declare your function:
void readInput(int *nums[], int *count);
the argument declaration int *nums[] is an array of pointers, not a pointer to an array which is what you call it like with &nums.
However, you don't need to pass a pointer to the array here. Arrays naturally decays to pointers to their first element. And when you declare an array as an argument to a function (like the declaration of nums in the function prototype above) it's really a pointer.
When you declare function arguments, a declaration like int nums[] is the same as int *nums.
So the declaration of readInput should really be
void readInput(int *nums, int *count);
If you do that change (including the function definition of course), and call it like
readInput(nums, &count);
then everything should work out fine.

C Warning passing argument 2 of ‘getopt’ from incompatible pointer type

I write a function with getopt() to get options from the command line. When I compile it, I get this Warning:
cc1: warnings being treated as errors
csim.c: In function ‘getArg’:
csim.c:157: error: passing argument 2 of ‘getopt’ from incompatible
pointer type /usr/include/getopt.h:152: note: expected ‘char * const*’
but argument is of type ‘const char **’
Here is the C code:
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(){
}
int getArg(int argc, char const *argv[], int *verbose, int *ps,
int *pE, int *pb, char *traceFileName){
int arg;
int argCount;
while ((arg = getopt(argc, argv, "vs:E:b:t:")) != -1){
switch (arg){
case 'v':
*verbose = 1;
break;
default:
printf("%s\n", "Illegal command arguments, please input again");
exit(-1);
break;
}
}
if(argCount < 4){
printf("%s\n", "Illegal command arguments, please input again");
exit(-1);
}
return 0;
}
The problem is, as the error says, you're passing a const char ** where a char * const* is expected. Specifically you're passing argv (which has the wrong type) to getopt. You could fix this by changing the type of argv.
int getArg(int argc, char * const argv[], int *verbose, int *ps, int *pE, int *pb, char *traceFileName)
It is the way your function has declared argv. You changed the constness. The error message is telling you what is wrong.

Resources