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).
Related
So, im trying to delete all entries of the linked list in my phonebook lab, and it's reading an error that I don't know how to fix. The error is reading "incompatible types when assigning to type char[50] from type 'int.'
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
typedef struct phonebook{
char first_name[70];
char last_name[50];
char phone_num[50];
}entry; //TypeDef to modify structure name
void addFriend(entry *, int *); //function prototype to add a friend
void dltFriend(entry *, int *, int);
void showBook(entry *, int *);
void alphaList(entry*, int*);
void findNum(entry*, int*);
void randSelect(entry*, int*);
void dltAll(entry*,int*);
void dltAll(entry*phonebook, int*count){
int i;
for(i=0; i< *count; i++)
{
do{
phonebook[i].first_name = '\0';
phonebook[i].last_name = '\0';
phonebook[i].phone_num = '\0';
break;
}
while (i <= *count);
}
printf("\nAll contacts deleted\n");
system("pause");
}
The error is reading "incompatible types when assigning to type char[50] from type 'int.'
The messages is due to this line:
phonebook[i].last_name = '\0';
\____________________/ \__/
This is a char[50] This is an integer
It makes no sense to assign an integer value to an array. If you want to turn last_name into an empty string do:
phonebook[i].last_name[0] = '\0';
Other notes:
A construct like:
do{
...
break;
}
while (i <= *count);
makes no sense as the break will end the loop after executing ... once. So simply remove the loop.
Also I would expect the function to set *count to zero.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
typedef struct phonebook{
char first_name[70];
char last_name[50];
char phone_num[50];
}entry; //TypeDef to modify structure name
void addFriend(entry *, int *); //function prototype to add a friend
void dltFriend(entry *, int *, int);
void showBook(entry *, int *);
void alphaList(entry*, int*);
void findNum(entry*, int*);
void randSelect(entry*, int*);
void dltAll(entry*,int*);
void dltAll(entry*phonebook, int*count){
int i;
for(i=0; i< *count; i++)
{
strcpy(phonebook[i].first_name,"NULL");
strcpy(phonebook[i].last_name,"NULL");
strcpy(phonebook[i].phone_num,"NULL");
}
printf("\nAll contacts deleted\n");
}
/*int main()
{
void dltAll();
return 0;
}*/
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
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'm new to C and I'm having a hard time understanding how to call methods with pointers. Currently this code should reverse a null-terminated string, but I get the errors
main.c:8:12: error: use of undeclared identifier 'sas'
char* N = sas;
^ main.c:10:10: warning: incompatible integer to pointer conversion passing 'char' to parameter of type
'char *'; remove * [-Wint-conversion]
reverse(*N);
^~ ./header.h:3:27: note: passing argument to parameter 'N' here EXTERN void reverse(char *N);
My actual code is this:
Main:
#include <stdio.h>
#include <stdlib.h>
#include "header.h"
int main(int argc, char *argv[])
{
char* N = sas;
reverse(*N);
}
reverse:
#include <stdio.h>
#include "header.h
#include <stdlib.h>
void reverse(char *str)
{
char* end = str;
char temp;
printf("this is *str: %c\n", *str);
if (str)
{
while (*end)
{
++end:
}
end--;
while (str < end)
{
temp = *str
*str++ = *end;
*end-- = temp;
}
}
}
header.h:
#define EXTERN extern
EXTERN void reverse(char *N)
thanks for the help and time!
int main(int argc, char *argv[])
{
char* N = "sas";
reverse(*N);
}
First you make N point to a string constant. Then you try to reverse what N points to. But since N points to a string constant, you're trying to reverse a string constant. By definition, constants cannot have their values changed.
First of all, there're many syntax errors within that piece of code:
'sas' what is that? your compiler thinks it's a variable but can't find any with that name. if you wanted to put a "sas" string, then:
char* N = "sas";
inconsistant brackets. More closing brackets than opening ones, and no opening bracket after declaring your function.
As I understand it, you are trying to reverse a string. This is just a slight modification of your code.
Full Source:
#include <stdio.h>
#include <stdlib.h>
void reverse(char *N);
int main(int argc, char *argv[])
{
char strSas[] = "sas";
reverse(strSas);
}
void reverse(char *str)
{
char* end = str;
char temp;
if(str) {
printf("this is *str: %c\n", *str);
while(*end) {
++end;
}
end--;
while(str < end) {
temp = *str;
*str = *end;
*end = temp;
++str;
--end;
}
}
}
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