hi all!!!!
im trying to do strcmp to a char in a structure and and char* and im getting a msg error: dereferencing pointer to incomplete type....
can somebody explain me what is that and give me an idea how to resolve it?
thanx in advanced!
hi all!!!!
im trying to do strcmp to a char in a structure and and char* and im getting a msg error: dereferencing pointer to incomplete type....
can somebody explain me what is that and give me an idea how to resolve it?
thanx in advanced!
If you want help with code, it's generally a good idea to post code. Otherwise, I trust what the compiler has told you is correct and as clear as possible.
if i have something like this.... and im getting an error msg on strcpy: " dereferencing pointer to complete type" , what is wrong?
im getting the same msg for most of my pointers and i dont know why, so i decided to try something like this simpler but i still dont see it.... can you help me:::
#include <stdio.h>
#include <malloc.h>
#include <string.h>
struct strs
{
char x;
};
typedef struct str strs;
int main()
{
strs *hola;
hola= (strs*)malloc(sizeof(strs*)*3);
char names[3][5] = {"DANA",
"RANI","SHIR"};
char *pnames[3];
int i;
for(i=0; i<3; i++)
{
pnames[i] = names[i];
strcpy(hola->x, pnames[i]);
}
system ("pause");
return (0);
}
Typo for starters:
typedef struct strs strs;
Structure element x is a single char: there is no room for it to be a string. So using strcpy to compare what isn't a string is not going to be right.
[edit]If I were to try to rename some variables to make more sense to me and make a few other changes, I might rewrite your code like this:
#include <stdio.h>
#include <stdlib.h> /* not <malloc.h> */
#include <string.h>
/** The number of strings and their maximum size. */
#define COUNT 3
#define SIZE 5
struct type
{
char text[SIZE];
};
int main(void)
{
const char names[COUNT][SIZE] = { "DANA", "RANI", "SHIR" };
struct type *array = malloc(COUNT * sizeof *array);
if ( array )
{
int i;
for ( i = 0; i < COUNT; ++i )
{
strcpy(array[i].text, names[i]);
}
/* Print array elements or do something else with it? */
free(array);
}
// system ("pause");
return 0;
}
I suspect you're compiling your C code as C++. That's not recommended unless you really know what you're doing. Anyway:
>#include <malloc.h>
<malloc.h> is archaic and non-standard. Use <stdlib.h> instead.
/* Wrong */
struct strs
{
char x;
};
typedef struct str strs;
Notice that your typedef is using a structure that was never defined? I don't see a struct str anywhere in your code, but there is a struct strs. You can use the same name for the structure and the typedef because they're in different name spaces[1]:
/* Yay! */
struct strs
{
char x;
};
typedef struct strs strs;
>strcpy(hola->x, pnames); hola->x
is a char, not a pointer to char. It's not a compatible type for use with strcpy.
>system ("pause");
You want to include <stdlib.h> for this function.
[1] Once again I'm risking confusion with C++ namespaces by introducing a space in the name. :D
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.