Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C incompatible pointer error

Tags:

c

pointers

I need help resolving this issue. Here is the code:

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

struct Person{
    char *name[100];
    char *nationality[100];
};

void put_values(struct Person *p, const char *name){
  strncpy(p->name, name, 500);
}

int main(int argc, char *argv[]){
    struct Person *person = malloc(sizeof(struct Person));
    put_values(person, argv[1]);
    free(person);
    return 0;
}

And here is the error message:

ex17t.c: In function ‘put_values’:
ex17t.c:19:3: warning: passing argument 1 of ‘strncpy’ from incompatible pointer type [enabled by default]
In file included from ex17t.c:4:0:
/usr/include/string.h:131:14: note: expected ‘char * __restrict__’ but argument is of   type ‘char **’

Any help or tips would be appreciated. Thanks!


1 Answers

Your structure should be (without the * characters):

struct Person {
    char name[100];
    char nationality[100];
};

The way you had it, name is actually an array of 100 character pointers rather then 100 characters. Unless your name is Juan Romirez Sancho Ricardo Agusti Donatello Alfonso ... Ramundo Ronaldo Bus Stop F’tang-F’tang Ole Biscuit Barrell and you want to be able to process each name independently, that's probably not what you're after.

It's also probably not wise to limit your strncpy to 500 characters when the field you're trying to copy in to has only 100 :-)

like image 69
paxdiablo Avatar answered Sep 20 '26 18:09

paxdiablo