C++ :: Iterating Through Struct Members?

Apr 4, 2013

Suppose I have a struct with 20 members. I want to assign each of those structs with values. Instead of accesing by explicit convention, i.e.:

Code: struct.member1 = 1;
struct.member2 = 2;
...
...
...
struct.member20 = 6;

Is there any way to encapsulate it in a for loop and use an iterator variable to indicate the member? i.e.:

Code: for (int i = 0; i < 20; i++)
struct.i = i;

The above is pseudocode but Im sure you get what Im trying to do?

View 2 Replies


ADVERTISEMENT

C :: Passing Struct Members To Execlp

Oct 2, 2014

I am trying to pass multiple args to g_signal_connect through a struct and it's members. I think the problem lies with execlp. I am trying to pass the struct members to it but I keep getting errors. No matter how I format execlp and the struct args, I always get an error. The current error I receive is Bad address. Like I have said before, no matter how I format execlp, I receive some sort of error.

Code: /*
Compile me with:
gcc -Wall sshfs-gtk -o sshfs-gtk `pkg-config --cflags --libs gtk+-2.0`
*/
#include <gtk/gtk.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <wchar.h>

static void destroy( GtkWidget *widget, gpointer data ) {

[Code] .....

View 4 Replies View Related

C++ :: Simultaneously Assign Value To Struct Members?

Feb 25, 2013

If I have a struct of some vector members, like

struct {
vector<double> a;
vector<double> b;
vector<double> c;
// ...... continue
} struct_test

How can I assign a value (push_back()) to all the vector members simultaneously, instead of do it one by one?

View 6 Replies View Related

C++ :: Accessing Private Members Of Same Struct Type

Mar 26, 2013

I've been reading the tutorials on Friendship and Inheritance [URL] ..... but I still don't understand why I can't access members of the same struct type.

bool wordBeginsAt (int pos) {
if (pos == 0)
return true;

///use the 'message' string
Message go;
return isAlphanumeric(go.messageText[pos]) && (!isAlphanumeric(go.messageText[pos-1]));
}

The code above is located in a source file, where the function isAlphanumeric passes a char value, and Message is the struct containing the string I want to access. Below is the declaration of the struct and string located in the corresponding header file.

struct Message{
.
.
.
private:
std::string messageText;
};

My frustration comes when I try to call and assign messageText like the tutorial does to its private members, but I keep getting an error saying I can't access the string because it is a private member. Is there a way to access the string without having to pass it through the function wordBeginsAt?

View 6 Replies View Related

C :: How To Generate List Of Struct Members Using Header File As Input

Jun 25, 2014

I need to create a list of all members of a struct using the sturct definition as input. The struct is NOT part of the program, but is the input to the program.

The struct that I am using changes over time as features are added. I need the complete, fully qualified field names to then generate a table with all names with their offsets, type and length.

This information then allows me to create readers of the data that will run on different architectures, compilers and operating systems.

The struct currently has 800+ lines and uses typedef and embedded structs.

Perhaps this could be done creating LEX, YACC, Perl, SED, AWK or other language system.

View 6 Replies View Related

C :: Why Size Of Struct Is Larger Than Sum Of All Size Of Its Members

Jul 11, 2013

I was wondering why, in C, the sizeof of a struct is larger than the the sum of all the sizeofs of it's members. It only seems to be by a few bytes, but as a bit of a perfectionist I fine this a bit annoying.

View 1 Replies View Related

C++ :: Iterating Over Either Row Or Column?

Jul 3, 2014

Suppose I want to represent an apartment building in terms of floors and rooms with a single data object. Each floor has the same number of rooms at any given time. During the course of the program, I will need to change the number of floors occasionally and will need to change the number of rooms on each floor occasionally. (Don't worry about the building permits needed to do this.) Now, I also want to be able to iterate over either each floor (think "row") or each room (think "column"). For example, I might want to program the equivalent of "Johnson, go fix all the front doors on the 8th floor" or the equivalent of "Johnson, go fix all the front doors to room B on each floor". (Yeah, I know, for the latter I could iterate over each floor and then access room B but that doesn't feel as "clean" as being able to iterate over a separate room/column.)

View 7 Replies View Related

C/C++ :: Iterating Over Either Row Or Column?

Jul 31, 2014

Suppose I want to represent an apartment building in terms of floors and rooms with a single data object. Each floor has the same number of rooms at any given time. During the course of the program, I will need to change the number of floors occasionally and will need to change the number of rooms on each floor occasionally. (Don't worry about the building permits needed to do this.)

Now, I also want to be able to iterate over either each floor (think "row") or each room (think "column"). For example, I might want to program the equivalent of "Johnson, go fix all the front doors on the 8th floor" or the equivalent of "Johnson, go fix all the front doors to room B on each floor". (Yeah, I know, for the latter I could iterate over each floor and then access room B but that doesn't feel as "clean" as being able to iterate over a separate room/column.)

View 2 Replies View Related

C++ :: Iterating Through A Vector Of Pointers

Sep 4, 2014

If I have a Vector of pointers, how can I iterate through that vector, to access the name function of each object in the vector? There seems to be a problem with my implementation, the output returns only random symbols.

Implementation of the name() function in Drug.cpp:

//name() is a function returning the name in the parent class
string Drug::name() {
string out = "Drug: ";
out += mName + " [ ";
//The problem is with this loop
for (int k = 0; k < mComponents.size(); k++)

[Code] .....

View 10 Replies View Related

Visual C++ :: Iterating Through Web Browser Controls?

Jan 31, 2013

there is a way to iterate through the controls on a webpage. For instance, I tried EnumChildWindows using the parent window of the browser, but I haven't had any luck - it returns nothing. s where I can start researching how to find the child controls?

I'm using Visual Studio 2010, programming in C++.

View 6 Replies View Related

C/C++ :: While Loop Application If They Continue Iterating Continuously?

Mar 17, 2015

what do I do if the while loop repeat iterating and do not stop running even if it did meet the conditions required?

View 3 Replies View Related

C++ :: Iterating Through Vectors - Print N Elements At Time

May 2, 2012

I am writing code to access vector elements as, to print first "n" elements of vectors out of its total N elements at a time.

For example, Total vector elements : N= 23
Want to print n elements at time : n =3

How this can be explored using vectors?

View 6 Replies View Related

C/C++ :: Sizeof (struct) Returns 6 More Bytes Than Actual Struct Size?

Sep 14, 2014

#include <stdio.h>
#define MAX_USERS 20
struct {
char ID[10];
char Name[40];
int Pos;

[Code] .....

I was attempting something weired with address to move data around when I discovered that the size of the array is not what I expected. I am passing this structure as &Users to a function that declares it as a void *, then I can deal with chunks of data (memmove) and not have to worry about index or things like that. However...sizeof is returning something I do not understand.

View 9 Replies View Related

C++ :: Creating A Struct Within Struct Node?

Feb 28, 2015

Im having trouble creating a struct within a struct node. the program suppose to hold students firstname, lastname, and gpa in a node therefore creating my linked list. Line 26 keeps saying that cannot convert parameter 2 from 'studentType to std::string

#include <iostream>
#include <string>
using namespace std;
struct studentType{
string firstname;
string lastname;
double gpa;

[code].....

View 2 Replies View Related

C :: Add Two Members Two Different Arrays Together

Jun 29, 2013

This is a function that is supposed to add two different arrays containing 9 items together, but it has a problem when it comes to doing any actual adding.

Code:

int * add_data(int x[], int y[], int z[])
{
int i;
char a[9];
for (i=1;i!=9;i++)
{
printf("%d =x
", x[1]+y[1]);
x[1] + y[1]= a[1];
}
return a;
}

View 5 Replies View Related

C++ :: Set And Get The Value Of Different Members In A Class

Jun 28, 2013

I am new to c++ programming and i have written a simple class program to display the name and duration of the project

#include<iostream>
class project {
public:
std::string name;
int duration;
};

[Code] ....

Now i am trying to the write the same program with the usage of member functions using set and get member functions. i have tried the following

#include<iostream.h>
class project {
std::string name;
int duration;
// Member functions declaration

[Code] ....

I am not sure whether above code logic is correct, how to proceed with it.

View 3 Replies View Related

C :: How To Access Name And File Name Members

Sep 8, 2014

How to access the name and file name members on line 42 and 43?

Code:

typedef struct {
char * name;
char * filename;
} FILES;
FILES * files[256];
}

[code]...

How to access the name and filename from within function?

*files[count].name = chTmp;

View 9 Replies View Related

C++ :: How To Define A Class Which Have Two Members

Sep 16, 2013

I want to define a class, which will have two members, for example, vaporPressureStatus and vaporPressure

enum vpStatus_t {nonesense, unknown, known, saturated};
class pore_t {
public:
vpStatus_t vpStatus;
double vaporPressure;
};

when vpStatus is nonsense and unknown, the vaporPressure should not have a value; and if I calculate out a value for vaporPressure, the vpStatus can be set as known.

I am wondering if there is any set, pair or other structure can hold this two members together, so that when I change one's value, the other guy will also change accordingly.

View 3 Replies View Related

C++ :: How To Qsort Array Of Structs By One Of Its Members

Mar 31, 2013

I am versed in qsorting struct members within a struct instance, as in the example below:

Code:
#include <iostream>
#include <stdlib.h>
using namespace std;
struct item_database {

[Code]....

What I CANT do however is to sort an array of structs by one of its members. This is a complete different problem even if it sounds similar.

In other words, the problem is this; Say we have a struct:

Code:
struct item_database {
int key;
string token;
};

Then we declare an array of this struct in the heap:

Code: item_database *idptr = new item_database[700000]; and initialize its values.

How would one go about sorting this heap array of structs by its member function "token"?

I used the following call to qsort (standard library) and call to the compare function but it doesnt work:

Code:
qsort (idptr, 1000, sizeof(string), compare);

Code: int compare (const void* a, const void* b){
if (*(char*)a >= *(char*)b)
return 1;
else
return -1;
}

Or in more lamens terms say we have the following stuct instances:
Key: Token:
1 Hello
2 World
3 How
4 Are
5 You

Then I need those structs to order according to token (alphabetically, increasing or decreasing depending whats in our compare function).....

View 3 Replies View Related

C :: How To Access Members Of Referred Array

Sep 11, 2014

files.c
Code:
#include "include/types.h"
#include "include/gaussian.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

[Code]....

I need to access the members .name and .filename to allocate memory and copy strings in there.

Code:
files[count]->name=malloc(64);
files[count]->filename=malloc(64); SIGSEGV error

How to make the reference working?

View 12 Replies View Related

C++ :: How Do API Call Back To Class Members

Jun 24, 2014

I've been creating an API and I'm now stuck on callbacks. There are many APIs that allow callbacks to class members(e.g. Windows API) using a void pointer to the object. I've searched the internet for hours and I can't find one example of how to use the "hidden object parameter" of an class method pointer that doesn't use std::function/bind or boost::function/bind. Any information on how API's like Windows API are able to use class methods as callbacks

View 6 Replies View Related

C++ :: Const Static Integral Members

Jan 16, 2014

I've been having a problem concerning the initialization of const static integral members with floating point calculations. I'll let his sample program do the explaining:

class Foo {
public :
Foo() {}
const static int samplerate = 44100;
const static unsigned short tempo = 120;

[Code].....

I know you can't initialize const static non-integral types on the same line on which they're declared, but I don't see why even an implicit cast to an integral type should be disallowed. I make my calculations using doubles, so I'm surprised that even though it should degenerate into an integer - it's still a problem for the compiler.

View 1 Replies View Related

C++ :: Why Class Members Private By Default

Apr 14, 2013

The reason that class members are private by default is because, in general, an object of a class should be a self-contained entity such that the data that make the object what it is should be encapsulated and only changed under controlled circumstances. Public data members should be very much the exception. As you’ll see a little later in the chapter, though, it’s also possible to place other restrictions on the accessibility of members of a class.

View 17 Replies View Related

C++ :: How Are Members Accessed In AMP Restricted Methods

May 17, 2013

Suppose I have a class "A", which has a method "void AMP_call()" that calls paralel_for_each in which another method, "float amp_function(float) restrict(amp)". When I call that method, can it then use members of "A"?

class A {
void AMP_call();
float amp_function(float) restrict(amp); // do something on a device
float allowed_variable;
std::vector<bool> not_allowed;

[Code] ....

Another way to frame my question, perhaps to make it easier to understand what I am after, would be that I want to know what happens if an amp-restricted method is called where the body of the class itself (which is not amp-compatible and afaik doesn't have to be since it's not passed to the device) may contain members that are not amp-compatible.

All of the msdn blogs I could find deal with which functions and methods can be called from within a parallel_for_each loop, but not with which variables are available to the lambda function itself.

View 9 Replies View Related

C/C++ :: Constructor To Initialize Each Members Of Array?

May 3, 2014

Need a C++ constructor to initialize each members of an array. how to give value for for each elements of an array declared as a class object according to the users input.

View 1 Replies View Related

Visual C++ :: Giving Values To All The Members?

Oct 15, 2013

Write a C++ program to implement the following description:

1. Define a global structure and name it GStruct with the following members:
a. X as integer
b. Y as integer.

2. Define a local structure inside the main and name it LStruct with the following members:
a. X as integer
b. Y[3] as GStruct

3. Inside the main declare two variables V1 and V2 of type LStruct.

4. Give values to all of their members by using input statement (cin).

5. If V1 equal V2 print "They are equal" else print "Not Equal".

#include <iostream>
using namespace std;
struct GStruct {
int x;
int y;
};
int main() {

[Code] .....

View 1 Replies View Related







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