I am using visual studio 2008 express edition.
A normal win32 console C project with the code below:
int main(void)
{
struct _addr_info
{
char name[30];
char street[40];
char city[20];
char state[3];
unsigned long int zip;
} addr_info;
addr_info.zip = 12345;
return 0;
}
Generally for structures intellisense will list the members. Here it doesnt however it compiles fine and at debugging i checked the data also gets entered properly. Am i doing something wrong.
even this code has same prob.
int main(void)
{
struct
{
char name[30];
char street[40];
char city[20];
char state[3];
unsigned long int zip;
} addr_info;
addr_info.zip = 12345;
return 0;
}
This code below also doesn't work.
int main(void)
{
struct _addr_info
{
char name[30];
char street[40];
char city[20];
char state[3];
unsigned long int zip;
} ;
struct _addr_info addr_info;
addr_info.zip = 12345;
return 0;
}
The code below works fine and list the members of the structure.
struct _addr_info
{
char name[30];
char street[40];
char city[20];
char state[3];
unsigned long int zip;
} addr_info;
int main(void)
{
addr_info.zip = 12345;
return 0;
}
This one too.
struct _addr_info
{
char name[30];
char street[40];
char city[20];
char state[3];
unsigned long int zip;
} ;
int main(void)
{
struct _addr_info addr_info;
addr_info.zip = 12345;
return 0;
}
I posted on MSDN forum and got the answer pointed by the link.
http://social.msdn.microsoft.com/Forums/en-US/Vsexpressvc/thread/8a22dc4a-3632-4cb9-92a3-63a18b55e7b6
Hope this helps.
If you're used to Visual C#'s intellisense then you'll be extremely disappointed in Visual C++'s as it's very buggy. You can try deleting the ncb files but you're better off using something like Visual Assist which is an intellisense replacement.
Answer copied from [ Why does Visual Studio not know the correct definition of this struct? ]
There's something about your situation described by Microsoft: http://support.microsoft.com/kb/822551
WORKAROUND: Microsoft strongly recommends that you use unique type
definitions.
Related
My intention: To loop through -- without using unions -- 30 members of a structure, all of type character array, storing in each the result of a call to itoa. In the following code, I name the structure members a-z,A-D. In the calling function, I initialize a string of those characters, called 'letters', then I try to loop through the structure members by referring to them with my increment variable as the index into letters. Then, I try to dump the contents of each member of the structure. **edit: I realize that the members wouldn't contain anything, given what code you can see. The problem seems to be with referring to struct members like this.
struct listArrays {
char a[10];
char b[10];
char c[10];
char d[10];
char e[10];
char f[10];
char g[10];
char h[10];
char i[10];
char j[10];
char k[10];
char l[10];
char m[10];
char n[10];
char o[10];
char p[10];
char q[10];
char r[10];
char s[10];
char t[10];
char u[10];
char v[10];
char w[10];
char x[10];
char y[10];
char z[10];
char A[10];
char B[10];
char C[10];
char D[10];
};
struct listArrays Ternaries;
int testTernary(){
char letters[30] = "ABCDabcdefghijklmnopqrstuvwxyz";
int i;
for(i = 0; i < 30; ++i){
dumpArray((Ternaries.(letters[i])), 10);
}
return 0;
}
The error I get is "expected identifier before '(' token."
Problems I have ruled out:
-The dumpArray function works fine.
-Looping through the letters works fine, outside the context of the
referral to struct members
Identifiers (names) only exist in the program text. Once compiled and linked, they no longer exist. So you can't "index" the struct for the letter. What you can do is:
struct listArrays {
char letters[30][10];
};
Now you can access these "letters":
dumpArray((Ternaries.letters[i]), 10);
I am trying to make changes to an array of structures. My code is as follows:
typedef struct {
char firstName[30];
char lastName[30];
char street[35];
char city[20];
char state[3];
int zip;
char phone[15];
int accountId;
} Customer;
struct Customer customer_list[];
//void copy_first_name(char data[], int i) {
// strcpy(customer_list[i].)
//}
int main(void) {
int _zip, _accountId;
char _firstName[30], _lastName[30], _street[35], _city[20], _state[3], _phone[15];
for (int i = 0; i < 10; i++) {
printf("Enter data for customer %d: \n", i);
printf("Enter First Last Phone: ");
scanf("%s%s%s", &_firstName, &_lastName, &_phone);
printf("\nEnter Address (Street City State ZIP): ");
scanf("%s%s%s%d", &_street, &_city, &_state, &_zip);
strcpy(customer_list[i].firstName, _firstName);
}
return 0;
}
I get the error message in the title, referring to customer_list. Can anyone help me out?
Change
struct Customer customer_list[];
to
Customer customer_list[10];
or (not recommended, but an example of the syntax)
struct Customer {
char firstName[30];
char lastName[30];
char street[35];
char city[20];
char state[3];
int zip;
char phone[15];
int accountId;
} customer_list[];
In your example, Customer is a typedef, equal to struct { ... }. It is not a named struct.
Some people like to do this, but I don't like it:
typedef struct Customer {
char firstName[30];
char lastName[30];
char street[35];
char city[20];
char state[3];
int zip;
char phone[15];
int accountId;
} Customer;
Customer customer_list[10];
or
struct Customer customer_list[10];
It would be better if they did:
typedef struct Customer_s {
char firstName[30];
char lastName[30];
char street[35];
char city[20];
char state[3];
int zip;
char phone[15];
int accountId;
} Customer;
Now it is clear that there is a difference between Customer and Customer_s.
Customer customer_list[10];
or
struct Customer_s customer_list[10];
You have [typedef][1] in struct so while declaring no need of struct keyword
do simply
Customer customer_list[10];
is enough.
or remove the typedef and declare struct like this ..
struct Customer {
char firstName[30];
char lastName[30];
char street[35];
char city[20];
char state[3];
int zip;
char phone[15];
int accountId;
} ;
struct Customer customer_list[10];
This is my code in which if student marks is greater than 85,scholarship status will be changed to sanctioned, but after updating it is not printing
#include<stdio.h>
#include<string.h>
struct scholor
{
char name[25];
int sem;
int marks;
char status;
};
void sanction(int m, char *s)
{
if(m>85)
{
char p[15]="sanctioned";
char *r;
r=p;
while(*r!='\0')
{
*s=*r;
s++;
r++;
}
*s='\0';
}
}
int main()
{
struct scholor s1;
scanf("%s%d%d%s",&s1.name,&s1.sem,&s1.marks,&s1.status);
sanction(s1.marks,&s1.status);
printf("%s",s1.status);
}
status is a single char but you are storing a string into it, effectively doing out of bounds access (undefined behaviour). Change it to an array and then you'll be able to copy.
struct scholor
{
char name[25];
int sem;
int marks;
char status[128];
};
and adjust the calls and passing (since status is an array now -- its name gets converted into a pointer t its first element when passed to functions):
scanf("%s%d%d%s",s1.name,&s1.sem,&s1.marks,s1.status);
sanction(s1.marks,s1.status);
printf("%s",s1.status);
Other suggestions:
1. Use a standard prototype for main such as: int main(void)
2. You could usr strcpy to copy the string as opposed to doing it yourself.
Your struct should have status as a character array not a character .Moreover when you scanf an array dont write & before because the name itself points to the assdress of the first element.Your corrected program is :
struct scholor
{
char name[25];
int sem;
int marks;
char status[16];
};
void sanction(int m, char *s)
{
if(m>85)
{
char p[15]="sanctioned";
char *r;
r=p;
while(*r!='\0')
{
*s=*r;
s++;
r++;
}
*s='\0';
}
}
int main()
{
struct scholor s1;
scanf("%s%d%d%s",s1.name,&s1.sem,&s1.marks,s1.status);
sanction(s1.marks,s1.status);
printf("%s",s1.status);
}
I have created a file msgbuf.h which is as follows:
//msgbuf.h
typedef struct msgbuf1
{
long mtype;
M1 *m;
} message_buf;
typedef struct msgclient
{
int msglen;
int msgtype;
char cp[100];
}M1;
And a program as check.c. Below prog is giving error that there is no M1. Why is it so? What mistake am I doing?
I think the contents of file "msgbuf.h" should get copied in the prog check.c and the program should run fine. Please let me know this.
//check.c
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include"msgbuf.h"
int main()
{
message_buf *sbuf;
sbuf=malloc(sizeof(sbuf));
sbuf->m=malloc(sizeof(M1));
sbuf->m->msglen=10;
printf("\n%d",sbuf->m->msglen);
printf("\n %d",sizeof(sbuf->m));
return 0;
}
Thanks :)
Simple, declare M1 before message_buf; .
typedef struct msgclient
{
int msglen;
int msgtype;
char cp[100];
}M1;
typedef struct msgbuf1
{
long mtype;
M1 *m;
} message_buf;
And read the keltar's comments below the question too.
You should declare M1 before using it:
//msgbuf.h
typedef struct msgclient
{
int msglen;
int msgtype;
char cp[100];
}M1;
typedef struct msgbuf1
{
long mtype;
M1 *m;
} message_buf;
I am creating an application which uses a vCard struct. Currently, this struct looks like this:
typedef struct {
char *version;
char **names;
char *formatted_name;
char *nickname;
char *organisation;
char *title;
struct { /* Emails */
char *global_type;
char *type;
char *address;
/* Meant to be a boolean which C doesn't have -_- */
unsigned preferred : 1;
} *emails;
struct { /* Phones */
char *type;
char *number;
unsigned preferred : 1;
} *phones;
struct { /* Addresses */
char *type;
char *street;
char *city;
char *postal_code;
char *country;
unsigned preferred : 1;
} *addresses;
time_t birthday;
struct { /* Custom Fields */
char *field_name;
union {
/* Single value */
int i;
float f;
double d;
time_t t;
struct {
char *global_type;
char *type;
char *address;
/* Meant to be a boolean which C doesn't have -_- */
unsigned preferred : 1;
} email;
struct {
char *type;
char *number;
unsigned preferred : 1;
} phone;
struct {
char *type;
char *street;
char *city;
char *postal_code;
char *country;
unsigned preferred : 1;
} address;
char *s;
/* Multiple values */
int *is;
float *fs;
double *ds;
time_t *ts;
struct {
char *global_type;
char *type;
char *address;
/* Meant to be a boolean which C doesn't have -_- */
unsigned preferred : 1;
} *emails;
struct {
char *type;
char *number;
unsigned preferred : 1;
} *phones;
struct {
char *type;
char *street;
char *city;
char *postal_code;
char *country;
unsigned preferred : 1;
} *addresses;
char **ss;
} field_value;
} *custom_fields;
} vCard;
This one is huge and takes much memory. I also use much pointers. Is there a better and cleaner way to declare this struct? Thanks.
Also, is it a good practise to use unions inside of structs and vice versa?
Yes! Split structs apart! (just like when there's a huge function.)
typedef struct {
char *global_type;
char *type;
char *address;
/* Meant to be a boolean which C doesn't have -_- */
unsigned preferred : 1;
} vCardEmail;
...
typedef struct { /* Custom Fields */
char *field_name;
union {
/* Single value */
int i;
float f;
double d;
time_t t;
vCardEmail email;
vCardPhone phone;
vCardAddress address;
char *s;
/* Multiple values */
int *is;
float *fs;
double *ds;
time_t *ts;
vCardEmail *emails;
vCardPhone *phones;
vCardAddress *addresses;
char **ss;
} field_value;
} vCardCustomField;
typedef struct {
char *version;
char **names;
char *formatted_name;
char *nickname;
char *organisation;
char *title;
vCardEmail *emails;
vCardPhone *phones;
vCardAddress *addresses;
time_t birthday;
vCardCustomField *custom_fields;
} vCard;