I'm using a structure within a structure like this in VS2010 (simplified:)
struct s_ptx
{
char xyz[33];
int newCount;
} ptx;
struct s_stream
{
struct ptx[20];
int count;
} Stream[20];
Everything is hunky dory until I try to examine this structure in the debugger:
eg, if I try to "watch" Stream[0].ptx[3].xyz, it gives some cryptic message ("CXX0058 Error: overloaded operator not found.")
I did a lot of searching on the Web, and found this is not an uncommon problem, and that
the only way to examine these sorts of structures is through adding a complex bit of
code to the autoexp.dat file.
It would be one thing if it were C code that the autoexp.dat file required but, alas, it is something else, and it would be very time-consuming to learn this new language just to do what I want to do.
So, my question:
does anyone know of a way to examine these sorts of structs in the VS debugger natively
does anyone know of some code that I could copy into my autoexp.dat to do the job
is anyone interested in writing this code as a service to me and all of mankind?
struct s_stream
{
struct ptx[20]; // **what is this ???**
int count;
} Stream[20];
i think 2nd structure should be like this
struct s_stream
{
struct s_ptx temp[20];
int count;
} Stream[20];
this code workd fine in my gcc
#include <stdio.h>
struct s_ptx
{
char xyz[33];
int newCount;
} ptx;
struct s_stream
{
struct s_ptx temp[20];
int count;
} Stream[20];
main()
{
printf("this works fine %s",Stream[0].temp[3].xyz);
}
Related
I am attempting to create a small ticket pricing system. The user inputs the people accompanying them to the theme park as a command line argument (senior/adult/child/student) including themselves (what they are according to that list) and I compare the command line arguments with the struct members named tier - this part will be completed later with pointers to check each one, but until then I'm just attempting to get this lesser version of the completed program to work and cannot seem to. It won't allow me to compare a struct member (string) with a command line argument. I'm finding rectifying this slightly confusing because the error message
strcmpare.c:17:29: error: use of undeclared identifier 'senior' if (strcmp(argv[a], senior.tier[a]) == 0)
isn't yielding any clues as to what I should do. Here's the code so far (I am using the cs50 course and sandbox, thus the cs50.h header file. Apologies in advance, I'm not sure what the equivalent is in C):
#include <cs50.h>
#include <stdio.h>
#include <string.h>
struct ages
{
char tier[7];
int price;
};
void structorganizer(struct ages senior, struct ages adult, struct ages child, struct ages student);
int main(int argc, string argv[])
{
for (int a = 0; a <= (argc-1); a ++)
{
if (strcmp(argv[a], senior.tier[a]) == 0)
{
printf("no errors");
return 0;
}
printf("scrutinise more");
return 1;
}
}
void structorganizer(struct ages senior, struct ages adult, struct ages child, struct ages student)
{
strcpy(senior.tier, "Senior");
senior.price = 10;
strcpy(adult.tier, "Adult");
adult.price = 30;
strcpy(child.tier, "Child");
child.price = 0;
strcpy (student.tier, "Student");
student.price = 20;
}
I'm finding rectifying this slightly confusing because the error message isn't yielding any clues as to what I should do.
On the contrary, it actually tells you EXACTLY what to do. That's not always the case with error messages, but here it is. The variable senior is not declared, so you need to declare it.
So where should you do that. You seem to want to use structorganizer to initialize things. Usually, global variables are a thing you want to avoid and there are certainly better ways of solving this, but just to quickly get things working. Remove all arguments from the function and declare global variables before, and call structorganizer first thing in main like this:
struct ages
{
char tier[8]; // Changed to 8 because you need one more than the string length
int price;
};
// Evil global variables, just to make code work with as little
// changes as possible
struct ages senior, adult, ages, child, student;
void structorganizer(); // Init the evil globals
int main(int argc, string argv[])
{
structorganizer();
// Continue as before
That should be enough to just get the code working, but you clearly need to spend more time with the basics here. The code you're writing right now is too advanced for you. No shame in trying though :)
There are absolutely cases where it's good to use an initializer function, but I cannot really say that this is one of them. I would declare them in main like this:
int main(int argc, string argv[])
{
struct ages senior = { .tier = "Senior", .price=10 };
struct ages adult = { .tier = "Adult", .price=30 };
struct ages child = { .tier = "Child", .price=0 };
struct ages student = { .tier = "Student", .price=20 };
There are some questions similar to this (namely, Define a struct with a member pointing to anothermember), but, given I'm complete noob at C, I cannot satisfy my doubt reading them, so I'm opening my own in the expectation of having some difference on it (if not, please point me to a proper question and close this as dupe).
What I'm trying to achive is self explanatory:
typedef struct {
int goals_as_visitor;
int goals_as_home;
int total_goals = goals_as_visitor + goals_as_home;
} team
I just want to store the total_goals struct member as permanently being the sum of the other two members. The above code doesn't compile. I don't know if I just haven't found how to do it just yet, or if this is not possible.
Any guidance?
No this is not possible. A C definition is roughly a definition of a storage space, no more. If you want to ensure that the field is always the sum of the first ones, then you need a discipline: each time you modify one of them, you'll have to modify the third. In this regard, OOP languages are better at this, C is too basic. Anyway you can try to use OOP style in C, something like:
typedef struct Team {
int goals_as_visitor;
int goals_as_home;
int total_goals;
} team;
void setGoalAsVisitor(struct Team *this,int value) {
this->goals_as_visitor = value;
this->total_goals = this->goals_as_visitor + this->goals_as_home;
}
...
setGoalAsVisitor(&team,666); // Roughly calling a method on team: aka team.setGoalAsVisitor(666) in Java style
...
and discipline yourself not to use the fields directly.
Note: you can hide many thing to enforce more the discipline, but alas C can let you make nasty things and violate the rules...
Here you are just defining a structures by telling the compiler what members it contains.
To use this structure first you have to declare the structure and then and then only will a memory be allocated for that structure.
Structure members cannot be initialised with declaration.
Definition would be like this:
typedef struct {
int goals_as_visitor;
int goals_as_home;
int total_goals;
} team
and declaration will be like:
team red;
team blue;
Now the structure has some memory allocated, and structure members can be accessed using dot [.] operator.
e.g. red.goals_as_visitor = 10;
Here is a sample code may be this can help you out
#include <stdio.h>
typedef struct {
int goals_as_visitor;
int goals_as_home;
int total_goals;
}team;
int main (void)
{
team red ;
red.goals_as_visitor=10;
red.goals_as_home = 5;
red.total_goals = red.goals_as_visitor + red.goals_as_home;
printf("%d %d %d\n", red.goals_as_visitor, red.goals_as_home, red.total_goals);
return 0;
}
You can refer below link for basics of structures [https://www.geeksforgeeks.org/structures-c/]
It is not possible in c.But the total_goals will always sum of goals_as_visitor and goals_as_home.So the better way is to use total_goals as a function pointer like this
#include<stdio.h>
/*structure and elements*/
struct team;
int tot_g(struct team *sthis);
typedef int (*tot_gol)(struct team *sthis);
struct team
{
int goals_as_visitor;
int goals_as_home;
const tot_gol total_goals;
}
const init={.goals_as_visitor=0,.goals_as_home=0,.total_goals=tot_g};
int tot_g(struct team *sthis)
{
return (sthis->goals_as_visitor+sthis->goals_as_home);
}
typedef struct team team;
/*structure and elements*/
int main()
{
team p=init;
p.goals_as_home=5;
p.goals_as_visitor=5;
printf("%d",p.total_goals(&p));
}
You must always initialize when a team object is defined with init.
Or
The simple way to do this is using a function like
#include<stdio.h>
typedef struct {
int goals_as_visitor;
int goals_as_home;
} team;
int total_goals(team var)
{
return (var.goals_as_home+var.goals_as_visitor);
}
int main()
{
team p;
p.goals_as_home=5;
p.goals_as_visitor=5;
printf("%d",total_goals(p));
}
You can use total_goals function parameter as const pointer also.
How can I use struct A to modify the data inside a struct B. Which has no name, just a type.
struct A {
struct B;
};
struct B {
int data;
};
Since this is for school, I can't change the code above. I can only use it. I tried something like this for my main but it doesn't work
int main (){
struct A myStruct;
myStruct.B.data = 3;
return 0;
}
Thanks in advance.
Edit: Sorry I was just trying to post this as fast as possible that's why I didn't post this with proper c syntax. Anyway, it's my fault for not being clear enough on my question.
I'm aware that my main doesn't work I just want to know if it's ever possible to access the data inside struct B without declaring a name for it inside struct A as I have above. This is the code I was given by a teacher, so I didn't want to modify the structs because I thought maybe she wants us to brainstorm a way to use it the way she wrote it.
The way iharob explains it works perfectly by declaring struct B before struct A, and actually giving a name to struct B.
Is it simply not possible to access that data inside struct B without giving it a name?
The code you posted is not even c code, it would not compile.
Your main mistake is that you don't need to use the struct name to access the member. This should be good
struct B
{
int data;
};
struct A
{
struct B member;
};
int main(void)
{
struct A instance;
instance.member.data = 3;
return 0;
}
I assume that you posted some sample code, don't do that. Post the actual code that has issues. The code you posted is completely invalid because some one of the definitions lack the type, you can't declare structs without using struct in c except if you typedef it. So please post the actual code the next time.
And don't build such complicated structs with struct members unless you really know what you are doing.
So I am still pretty new to C programming. I have learned Python though, so I am familliar to some of the codes.
For instance when I create a function in python, I am able to make it general and usable for different classes.
I want to do something similar here. I have two structs which look practically the same. I want to use the same function for both structs, but ofcourse I cant send in the struct name as an argument into the function. What do I do instead?
For now dont worry about what the function does. Its the principle of being able to use two structs in the same function that counts for me. If this is a totally wrong perspective, then I am sorry but this was my first thought when coming upon this problem.
typedef struct{
int number;
struct node *next;
}struct_1;
struct node *head;
typedef struct{
int number;
struct node *next;
}struct_2;
void main()
{
int number1 = 10;
int number2 = 20;
function(number1);
function(number2);
}
void function(int x, struct) // Here is where I want to be able to use 2 different structs for the same function
{
struct *curr, *head;
curr=(node1*)malloc(sizeof(node1));
printf("%d", curr->number);
}
You could have two instances of one structure.
The function can accept either instance and process it as needed.
typedef struct{
int number;
struct node *next;
}mystruct;
void function(int x, mystruct *eachstruct);//prototype
int main()
{
int number1 = 10;
int number2 = 20;
//declare two instances of mystruct
mystruct list_1 = { 0, NULL};
mystruct list_2 = { 0, NULL};
// call the function with one or the other instance of mystruct
function(number1, &list_1);
function(number2, &list_2);
}
void function(int x, mystruct *eachstruct)
{
//do stuff in function
eachstruct->number = x;
if ( eachstruct->next == NULL)
{
//do more stuff
}
}
C does not use duck typing as Python does so you cannot pass one structure that looks like other, completely unrelated structure as if it was this other structure.
Unfortunately C cannot do what you want.
Your options are:
Refactor the code to use the same struct type for all items.
Pass the fields of interest in the structs directly to the functions
Write code to marshal the similar structs to a common struct.
Play fast and loose with the type system and arrange shared elements the same way in the two different structs and cast your pointers.
If you just want a linked list check out how code re-use is achieved in the linux kernel
Answer: No, you cannot do it directly. Welcome to static typing.
There is a way to achieve something similar by using our beloved void * and some castings but, believe me, it is not what you want to do. If you really want to do it ask directly for it. You have been warned.
In the given code snippet, I expected the error symbol Record not found. But it compiled and ran fine on Visual Studio 2010 Compiler. I ran it as a C program from Visual Studio 2010 Command Prompt in the manner -
cl Record.c
Record
Now the doubt is, doesn't typedef check for symbols ? Does it work more like a forward declaration ?
#include "stdio.h"
#include "conio.h"
typedef struct Record R;
struct Record
{
int a;
};
int main()
{
R obj = {10};
getch();
return 0;
}
You can always refer to undefined structures, which is a typical way to implement linked lists, after all. They just have to be defined when you want to make use of their fields.
This page contains some details.
C does not find the symbol Record because it is declared later on the code, like if you were trying to use a function you declare past on the code without defining its prototype.
You can also combine the two declarations, and then it becomes:
typedef struct Record
{
int a;
} R;
It also works and, in my opinion, even better, not because it can be faster, but because it is smaller.
typedef must be used after its first parameter has been defined.
struct Record
{
int a;
};
typedef struct Record R;
or
typedef struct Record
{
int a;
} R;
If you need to use struct Record within the struct, just use struct Record:
typedef struct Record
{
struct Record *next;
}
typedef struct Record R;