C/C++ :: Value Assignment To Structure Member Inside The Structure?

Oct 7, 2014

Is it possible to assign a value to structure member inside the structure.like.....

struct control{  
char tbi:2 =0;
char res:1 =0;
};

View 6 Replies


ADVERTISEMENT

C :: Set Struct Member Variable For Structure Inside Def

Mar 12, 2014

This is with Linux gcc

Code:
typedef struct _a
{
int id;
} a;
typedef struct _b
{
a my_a;
my_a.id = 1; // error: expected specifier-qualifier-list before "my_a"
} b;

I get error: expected specifier-qualifier-list before "my_a"

I must set the id for the kind of struct created inside the struct def because main() will be casting based on this id. Thats how I will know which structure b contains by it's id, there could be hundards of different structs with different values I will cast to the correct one and know it's members by it's id. How do I ?

View 10 Replies View Related

C++ :: Composition And Member Access Inside Composed Structure

Jan 18, 2013

I have a question regarding composition and accessing members "deep" inside the composed structure. For example;

class A {
private:
int m_myInt;
public:
int myInt() const {return this->m_myInt;};
void myInt(int newInt) {this->m_myInt = newInt;};

[Code] ....

Now, from somwhere I have access to an object of type B where I want to update the A::m_myInt. How would you do this without "breaking" the whole purpose of private/public members?

B myB;
myB.m_a.myInt(3); // Not allowed, and not desireable

I thought about implementing access through functons kind of like;

A & B::a() {return this->m_a;};
myB.a().myInt(3);

but I'm worried that this exposes my B::m_a-object too much. This would allow

myB.a() = A();
, right?

The following is a more desireable way of acces, but doesn't work for updating;

A const & B::a() {return this->m_a;};
myB.a().myInt(3); //Disallowed? myInt(int) is non-const.

What about this? Is this a good way of doing it?

class A {
private:
int m_myInt;
public:
int myInt() const {return this->m_myInt;};

[Code] ....

I guess it works? It would lead to a lot of data shuffling in case of larger sub-components.I would really like to do the following without exposing my components so much:

B myB;
myB.a().myInt(3);

Can it be done?

View 11 Replies View Related

C/C++ :: Not Able To Initialize Structure Variable Inside Main Whose Structure Defined GL

Aug 27, 2013

I am trying to run a programme implementing a function with structures in c... which is:

#include<stdio.h>
#include<conio.h>
struct store  {
        char name[20];
        float price;    
        int quantity;

[Code] .....

View 1 Replies View Related

C++ :: How To Use Structure Pointer Through A Structure Public Member Definition

Dec 7, 2014

Why doesn't this compile?

struct hi(){
void other();
}histructure;

void hi::other(){
std::cout << "Hi!" << std::endl;

[Code] ....

Makes no sense the structure is written before the structure member function is called so why is there compile errors ??...

View 3 Replies View Related

C :: Uninitialized Variables Inside A Structure

Aug 7, 2013

Let me say I have a structure

Code:
struct time{
char hours;
char minutes;
char seconds;
char dummy;
};

I have kept dummy as the data to be aligned.I will update hours, minutes, and seconds , but will not use dummy in any case. If I don't initialize 'dummy' does it make any errors ? Do I need to initialize hours, minutes, seconds as well before I use the structure ? If so is there any particular reason ?

View 7 Replies View Related

C++ :: How To Define Enumeration Inside A Structure And Use It In Other File

Mar 5, 2014

I want to define enumeration inside a structure and use it in some other file

for eg

header.h
typedef struct dummy {
typedef enum {
ZERO,
ONE,
MAX
} NUMBERS;
// some member variables
} DUMMY; // end of the structure

otherfile.cpp

in this file i want to access both enumerations(for eg) ONE as well as the type NUMBERS.

View 1 Replies View Related

C/C++ :: Not Displaying Values Inside Structure Array (GCC)?

Apr 22, 2014

#include <stdio.h>
#include <string.h>
#define MAX 22
struct inven {
int Iid;
double uprice;
int uoh;
char name[MAX];

[code].....

doesn't seem to want to display the disp* fucntions at all

View 2 Replies View Related

C/C++ :: Accessing Pointer Inside A Nested Structure

Mar 19, 2014

i had a structure as follows:
struct a{
 int x;
 struct b{
  int y;
  b *next;
 }b;
};

when i try to access as follows:
struct a *a;
a->b=a->b->next;

i got the following error: base operand of ‘->’ has non-pointer type

View 1 Replies View Related

C++ :: Malloc For Union Inside Structure Fails

Dec 27, 2012

I have some problem while allocating memory to a union inside structure. Below is the code i am using

ON SYSTEM1:

This works fine

ON SYSTEM2:

compiler complains saying "need structure or union type" while allocating MYSTRUCT1.

If I change:

shreyas[0].UnionAttr.struct1 = (MYSTRUCT1 *) malloc (sizeof(MYSTRUCT1)
to
shreyas[0].UnionAttr->struct1 = (MYSTRUCT1 *) malloc (sizeof(MYSTRUCT1)

This compiler on SYSTEM2 is happy. but second way does not look correct to me and compiler on system 1 complains about it. Which is the correct way to allocate memory?

If first one is correct then what should i look in for to avoid this error? Could this be an issue with compiler on SYSTEM2? If i use second method on SYSTEM2 code segfaults during malloc.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct mystruct1 {
int a;
int b;

[Code] ....

View 3 Replies View Related

C++ :: Why Is Structure Member Not Displaying

Jan 7, 2015

I have started working with structures so here's a side project from my text book. It's purpose is fairly simple; it asks for the sales of each quarter of the year from 4 different divisions and then calculates the average quarterly sales and total annual sales and finally displays all the data. My problem is that in the function "displayCompanyInfo" the statement

std::cout << "Division " << R.division_name << std::endl;
does not display the name of the division. With that in mind here is the code:
#include <iostream>
#include <string>
struct CompanyInfo
{

[Code]....

As you can see the last part of the output has statements that say "Division" however they do not say the name of the division afterwards. I don't understand why that is?

View 2 Replies View Related

C :: How To Assign String Hello To Structure Member

Feb 17, 2015

See the simple code below. The compiler gives message:

assigning to type "char[20]" from type "char *".

I've tried everything else that seems reasonable but none works. How can I assign string Hello to the structure member?

Code:
int main() {
struct strc1
{
char msg1[20];
char msg2[5];
}
talk;
talk.msg1 = "Hello";
}

View 5 Replies View Related

C++ :: Dynamic Table As A Structure Member?

Mar 24, 2014

is it possible to make something like that?

struct type_name
{
char Status[i];
string Status[j];
.
.
} object_names;

The problem is I dont know how many statuses my object will have. Is it possible to make it in an dynamic array?

View 3 Replies View Related

C++ :: Set Member To Same Value In All Instances Of Templated Structure

Sep 3, 2013

template <typename T> struct avl_tree {
T data;
int balance;
struct avl_tree <T> *Link[2];
static int (*comp)(T, T);
};

In main, I have a function like so:
int compare(int a, int b) {
return ( a - b );
}

Now how do I assign the function pointer in my avl_tree class to the compare function?

I did this:
int (avl_tree<int>::*comp)(int, int) = compare;

But I got the compiler error:
Tree_Test.cc: In function ‘int main()’:
Tree_Test.cc:27:42: error: cannot convert ‘int (*)(int, int)’ to
‘int (avl_tree<int>::*)(int, int)’ in initialization

View 12 Replies View Related

C/C++ :: Assigning Value From Struct Member To Outside Structure

Jun 20, 2013

I have the code bellow, and i print the data but cannot assign it to global variable.

struct frequency_data {
 char frequency[10];
 int32 frequenca_mesatare;
 int16 PWM_F;
 int32 PR2;

[Code] ....

View 5 Replies View Related

C++ :: Pass Pointer To Member Function (polymorphic Structure)?

Jan 3, 2015

I am trying to create a callback system for input events in my game engine.

Here is a cut down version of the EventManager.h file

#include "Controls.h"
#include "Object.h"
enum MouseEventType{PRESSED, POINTER_AT_POSITION, PRESSED_AT_POSITION };

[Code].....

This works if the function pointer being passed to the event manager is not a member function.

I have two other classes Scene and Object that could potentially use this EventManager to create callback events. Scene and Object are both pure virtual objects. How can I pass a pointer to a member function of the child classes of both Scene and Object? I am fine with just having two separate watchEvent functions for Scene and Object but how do I pass the type of the Object or the type of the Scene? The child classes are unknown as they are being created by someone using this game engine.

For example, if I want to make a player object it would be something like

class PlayerObject : public Object{...};

Now that PlayerObject type has to find its way to PlayerObject::functionToCall(). I think I need templates to do this but, since I never used them before

This is how I intend to use this

class OtherScene : public Scene{
void p_pressed(void){
//pause
}

[Code].....

View 6 Replies View Related

C :: How To Create A Structure That Pointing To Another Different Structure

Mar 17, 2013

how I can create a structure that pointing to another different structure. And also passing structure to function.

View 3 Replies View Related

C :: Calling Function Via Function Pointer Inside Structure Pointer

Mar 14, 2013

I'm trying to call a function via a function pointer, and this function pointer is inside a structure. The structure is being referenced via a structure pointer.

Code:

position = hash->(*funcHash)(idNmbr);

The function will return an int, which is what position is a type of. When I compile this code,

I get the error: error: expected identifier before ( token.

Is my syntax wrong? I'm not sure what would be throwing this error.

View 3 Replies View Related

C++ :: Accessing Inside Structure Via Struct Pointer To Struct Pointer

Jun 5, 2012

"
#include <stdio.h>
struct datastructure {
char character;
};
void function(struct datastructure** ptr);

[Code] ....

These codes give these errors:

error: request for member 'character' in '* ptr', which is of non-class type 'datastructure*'
error: request for member 'character' in '* ptr', which is of non-class type 'datastructure*'

These errors are related to
"
*ptr->character='a';
printf("Ptr: %c",*ptr->character);
"

I want to access "character" data inside the structure "trial" by a pointer to pointer "ptr" inside function "function",but I couldn't find a way to do this.

View 3 Replies View Related

C++ :: Assignment Operator But With Some Member Exceptions

Jan 9, 2015

The task is to use the assignment operator of a class, but change all the data except certain ones. For example, below we are to assign all Person data of 'other' except for 'name' and 'ID':

#include <iostream>
#include <string>
struct Person {
std::string name;
int ID, age, height, weight;

[Code] .....

Name = Bob
ID = 2047
Age = 38
Height = 183
Weight = 170

Name = Frank
ID = 5025
Age = 25
Height = 190
Weight = 205

Bob pretends to be Frank, but keeps his name and ID.

Name = Bob
ID = 2047
Age = 25
Height = 190
Weight = 205

But I think the way I did it is pretty lousy (note the wasted steps changing the name and ID only to revert them back? So the ideal solution should require no wasted steps, unlike the method above, and changes to what the exclusions should be should be in only one place (not two like above). Of course, we assume that Person shall have many, many data members (and constantly increasing), so that simply defining Person::operator= (const Person& other) to handle all data except for 'name' and 'ID' is out of the question.

View 3 Replies View Related

C++ :: Member Function Pointer Assignment?

May 15, 2013

I have a class with member functions and a pointer like this:

Code:
class MyClass{
private:
typedef void(MyClass::*memFnPtr_t) ();
public:
memFnPtr_t fnptr;
void fn1();
void fn2();
};

Now in a regular function I create an instance of the class and try to assign the pointer:

Code:
MyClass mc;
mc.fnptr = &mc.fn1;

The compiler does not like it and suggests this instead:

Code:
mc.fnptr = &MyClass::fn1;

This seems to work but what if I have two instances of the class:

Code:
MyClass mc1, mc2;

How does the compiler know to distinguish between

Code:
mc1.fn1

and

Code:
mc2.fn1

when the assignment now looks identical:

Code:
mc1.fnptr = &MyClass::fn1;
mc2.fnptr = &MyClass::fn1;

View 14 Replies View Related

C++ :: Extractor Overloading And Member Wise Assignment For Cartesian Class

Apr 6, 2014

I am making a program with a Cartesian class. I want the user to be able to input 2 coordinates, but when I run it it doesn't ask for any values to be entered. It gives this output Please enter the first coordinates: Please enter the second coordinates:

(4.86129e-270, -1.97785e-41)
(4.86143e-270, -1.97785e-41)

Code:
#include <iostream>
#include <istream>
#include <ostream>
using namespace std;

[Code] ....

Also, I want to add a memberwise assignment function to assign the values of coord1 to coord2. How would I go about doing so?

View 7 Replies View Related

C++ :: Static Variable Inside A Member Function

Jul 20, 2013

Say you had:

class Foo{
public:
//...
void funky();

[Code] .....

Would each instance of Foo create a new counter variable, or would it remain the same for all of them, i.e. baz.funky() would always use the same counter variable? What if the class was a template?

View 3 Replies View Related

C :: Using Structure In Union

May 27, 2013

I'm trying to use a structure in union in the following format:

Code:
union data
{
unsigned char All[10] ;
struct data_pkt
{
unsigned char ack;
unsigned short status;
unsigned short data_length;
unsigned char Data[5];
}format;
}adb; adb.

All has 10 bytes which is equivalent to the structure bytes. ie 6 bytes if unsigned char and 2 short i.e 4 bytes. Thus total 10 bytes is given to adb.All. When I print the struct size I get 12 bytes. This creates problem in obtaining data in union. According to the program:

adb.format.ack should have the address of adb.All[0]
adb.format.status should have the address of adb.All[1]
adb.format.data_length should have the address of adb.All[3]
adb.format.Data[0] should have the address of adb.All[5]

But in actual case this is how memory is allocated:

adb.format.ack assigned to the address of adb.All[0]
adb.format.status assigned to the address of adb.All[2]
adb.format.data_length assigned to the address of adb.All[4]
adb.format.Data[0] assigned to the address of adb.All[6]

Why this is happening? How can I solve this?

View 9 Replies View Related

C :: Set Every Array Value In Structure To Zero

Nov 22, 2013

i'd like to ask if it's possible to fill every array in structure to zero. (Without using too much cycles)Something like this (I know that it doesn't work):

Code:

typedef struct {
char name[30], sname[30], adress[30], day, month; int year; char number[9], email[30];
} person;
void create_list(person z[100])
}

[code]....

View 2 Replies View Related

C++ :: Map With Pointer And Structure

Nov 28, 2013

Program to create client server interaction for store data into map using structure and pointer(without memory leak).

View 1 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved