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
Related
Can anyone explain to me how this code snippet works?
typedef int (*compare)(const char*, const char*);
It is a declaration of an alias for the type pointer to function that has the return type int and two parameters of the type const char *.
typedef int (*compare)(const char*, const char*);
Using the alias you can declare a variable of the pointer type as shown in the demonstration program below
#include <string.h>
#include <stdio.h>
typedef int (*compare)(const char*, const char*);
int main( void )
{
compare cmp = strcmp;
printf( "\"Hello\" == \"Hello\" is %s\n",
cmp( "Hello", "Hello" ) == 0 ? "true" : "false" );
}
where strcmp is a standard C string function declared like
int strcmp(const char *s1, const char *s2);
and the pointer (variable) cmp is initialized by the address of the function.
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).
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.
I'd like to assign a pointer to a const char* array like this:
#include <stdio.h>
const char *keyContainer[2]= {"test", "test2" };
const char *keyPtr = &keyContainer;
int main(void)
{
printf("%s\n", keyPtr[0]); //test
printf("%s\n", keyPtr[1]); //test2
return 0;
}
keyPtr contains the address of keyContainer but I can't get access to the content of keyContainer.
When you use this, you get the warning "Initialization from incompatible pointer type [enabled by default]", and that's because keyContainer is char *[2], which we will say can be similar (not the same !) as char **. So you need to use const char **keyPtr = keyContainer;.
For example :
#include <stdio.h>
const char *keyContainer[2]= {"test", "test2" };
const char **keyPtr = keyContainer;
int main(void)
{
printf("%s\n", keyPtr[0]);
printf("%s\n", keyPtr[1]);
return 0;
}
Hope this helps !
I am starting to learn about pointers in C.
How can I fix the error that I have in function x()?
This is the error:
Error: a value of type "char" cannot be assigned to an entity of type "char *".
This is the full source:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
void x(char **d, char s[]) {
d = s[0]; // here i have the problem
}
void main() {
char *p = NULL;
x(&p, "abc");
}
In function x() you pass d which is a char ** (a pointer to a string pointer), and char s[] (an array of char, passed as similarly to a pointer to char).
So in the line:
d = s[0];
s[0] is a char, whereas char **d is a pointer to a pointer to char. These are different, and the compiler is saying you cannot assign from one to the other.
However, did your compiler really warn you as follows?
Error: a value of type "char" cannot be assigned to an entity of type "char *"
Given the code sample, it should have said char ** at the end.
I think what you are trying to make x do is copy the address of the string passed as the second argument into the first pointer. That would be:
void x(char **d, char *s)
{
*d = s;
}
That makes p in the caller point to the constant xyz string but does not copy the contents.
If the idea was to copy the string's contents:
void x(char **d, char *s)
{
*d = strdup(s);
}
and ensure you remember to free() the return value in main(), as well as adding #include <string.h> at the top.
A more proper way would be to use strcpy:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
void x(char **d) {
*d = malloc(4 * sizeof(char));
strcpy(*d, "abc");
}
int main() {
char *p;
x(&p);
printf("%s", p);
free(p);
return 0;
}
Outputs: abc