C++ :: Why Can Initialize Array Nested In Structure Like Array

Feb 7, 2015

Example :
Code:
struct x {
int v[4];
};
const x test = { 0, 1, 2, 3 };

Why can I do this? How does the compiler know to write to this in the proper way? I get that v would be contiguous. Does that have something to do with it?

View 2 Replies


ADVERTISEMENT

C++ :: Passing Nested Structure Array To Function

Feb 27, 2014

I've been able to write the program completing all requirements except for one... creating a function that accepts a nested structure array. Here's the program:

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
struct PayInfo {
double hours; // Hours worked
double payRate; // Hourly payRate

[Code]...

I don't even know where to begin. Specifically, concerning all the aspects of the function.

View 5 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/C++ :: How To Initialize Array Of Pointers To Array Of Characters

May 21, 2014

I am trying to initialize an array of pointers to an array of characters, I can do it in 3 lines but I really want to do it in one line at the same time keeping the #define.

3 lines initialization (can compile)
======================
#define A 1
#define B 2
char row1[] = {A|B, B, A};
char row2[] = {B, A};
char *test[]= {row1, row2};

1 line initialization (failed)
===============================
char *test[] = { {A|B, B, A}, {B, A} }; // <- how do i do this??

I do not want this because it waste ROM space
=============================================
char test[][3] = { {A|B, B, A}, {B, A} };

View 18 Replies View Related

C++ :: Vector In Nested Structure

Nov 26, 2013

I have a nested record structure in C++ that contains a FileHeader record, a RecordHeader record and a DataRecord record. The last item is an array of unknown size (at compile time). I have to open a file and read the array size and then create the array.

I have worked on this for some time and can't figure out how to make it work. I know the sample data I am using has 85 records. I insert 85 into the array size and it works fine. But how do I create a dynamic array or define a vector within a nested structure?

1. What is the best (easiest) method to accomplish this (array or vector)?
2. How would it be implemented/how do you add data to it?

PreviousLogRecord.FaultRecord.push_back(field1); // does not work
PreviousLogRecord.FaultRecord[recordNumber].field1 = somedata; // works with 85 in array size.
struct LogFileHeaderRecordType {
QString field1;
int field2;

[Code] .....

View 3 Replies View Related

C++ :: Initialize Array In A Constructor?

Dec 6, 2013

There are two class.How can I initialize an array of different class in a constructor?

class Ticket{
private:
int ID;

[Code]....

I have to provide a no-argument constructor (Cinema();)to initialize the ticket array and give the right ticket ID.

View 1 Replies View Related

C/C++ :: Initialize Array With Tiles?

May 8, 2014

imagine you have a world class. Than you have a tile class. Now, in the world class is a array with a lot of tiles and I want to initialize them with my imagination(For example I want a grass floor). But how I can do this, the array can only be create with the standard constructor. But it would be stupid and not very fast, that the tiles are first initialized with the standard constructor and than overridden to build the world. Whats the best way to initialize such a array ?

View 12 Replies View Related

C :: How To Access Nested Structure Using Pointer

Jan 22, 2015

I need to know how to access the nested structure using pointer, i mean i know how to do that when i have simple structure, for example:

Code:
struct person{
char fname[16], lname[16];
int age;
}

for this example i can use

Code: (*pointer).age
or
Code: pointer->age

But if i have structure like this:

Code:
struct date{
int day, month, year;
}
struct person{
char fname[16], lname[16];
struct date birthDate;
}

Then how can i access the birthDate using pointer?

View 2 Replies View Related

C++ :: How To Take Size Of Array And Initialize It On Heap

Feb 6, 2014

I am creating a class that has a private array on the heap with a constructor that takes the size of the array and initializes it on the heap. Later I have to make a deconstructor delete the space and print out free after.In my code, I was able to heap a private array and make a deconstructor, but I don't know how to take the size of the array and initialize it on the heap. My guess is this:

int* size = new int();

Also when you initialize size on the heap, don't you also have to delete it too? If so, where, in the code, do you do that? Here is my code so far.

Class Student {
private:
int size;
int* array = new int[size];
public:
Student(); // Constructor
~Student(); // Deconstructor

[code]....

How do you make a constructor that takes the size of the array and initializes it on the heap

Student::~Student()
{
delete[] array;
cout << "Free!" << endl;
}

View 1 Replies View Related

C++ :: Can't Initialize String Array In Constructor

Jul 22, 2013

I am currently practicing designing classes. In one exercise, I am trying to store 15 words in an array, and randomly print one (using the rand() functions and seeding it with crime. I have a header file, a respective .cpp file, and a main .cpp file. When I try to compile the code using g++ GuessWord.cpp UseGuessWord.cpp -o UseGuessWord, I get the following error in my constructor: expected primary-expression before ‘{’ token

Here is my code:

header file (GuessWord.h):
#ifndef GUESSWORD
#define GUESSWORD
#include <string>
using namespace std;

[code].....

View 2 Replies View Related

C++ ::  How To Initialize Function Prototype Of Array With Zero

Apr 23, 2014

We can initialize normal function prototype's parameters with zero like this:-

void output(float = 0.0, int = 0);

or

void output(int = 0, int = 0, double = 0.0);

But how do you do the same for a pointer array or simply an array?

Assume that second parameter has to be an array.

I have tried the following and it does not work:-

void output(float = 0.0, int = 0);
void output(float = 0.0, *int = 0);
void output(float = 0.0, int* = 0);
void output(float = 0.0, int[] = 0);
void output(float = 0.0, int []);

But if I skip the default declarations altogether, it works.

like:

void output(float, int []);

or

void output(float, int*);

how can I do it by explicitly writing zero, just like the first cases?

View 2 Replies View Related

C/C++ :: String Out Of Bound - How To Initialize Array

Mar 7, 2014

I'm a newbie to C++ and I am writing a code that searches strings and checks their value. I'm mostly working with if statements.

My code tends crash on Dev C++, even when I haven't recently saved any changes or compiled it, citing "memory access errors".

I recompiled in visual studio, and visual studio told me I have string out of bound errors.

My code is very long... but most of it is copy and pasted with slightly different conditions.

I believe the problem lies in how I initialize arr[4] ??

#include <cstdlib>
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main() {
cout << "Hall symbol -- generator matrix program" << endl;
// Setting up a data structure bc a normal array cannot handle the data for the spacegroups

[Code] ....

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

C/C++ :: How To Initialize Array Of Structures At Runtime

Aug 26, 2014

I somewhere read "You cannot initialize a structure like that at run time."

Example:
struct item_info {
      char itemname[15];
      int quantity;
      float retail;
      float wholesale;

[Code] ....

But if you want to assign values at run time then you have to do it manually like:

strcpy(item[0].itemname, "rice");
item[0].quantity = 10;
item[0].retail = 40;
item[0].wholesale = 30;

I tried in internet but am unable to know the differences. I want to know the difference between those two in terms of run time and compile time. Explanation required also for below one. Is this run time or compile time? How does we actually decide which is run time and which is compile time!

struct item_info {
      char itemname[15];
      int quantity;
      float retail;
      float wholesale;
      //int quatityonorder;

[Code] ....

View 3 Replies View Related

C++ :: How To Initialize Array Of DirectX9 Objects

Jan 3, 2015

I've got an error saying that there is an access violation at 0x0000040 error no 0xC0000005

I've searched the net, saying the error may be due to an uninitialzed variable.I debugged the code, and found out that an object created from directx D3DXMATRIXA16 is uninitialized.With values ??,??,?? in each element

pMeshContainer->pBoneMatrices = new D3DXMATRIXA16[g_NumBoneMatricesMax];

here, pMeshContainer is a variable passed into a function called GenerateSkinnedMesh by pointer

GenerateSkinnedMesh(..., D3DXMESHCONTAINER_DERIVED *pMeshContainer)

Then in turn, GenerateSkinnedMesh is called within a callback from a DirectX9 API called

ID3DXAllocateHierarchy

Code:
class CAllocateHierarchy : public ID3DXAllocateHierarchy {
public:
STDMETHOD( CreateFrame )( THIS_ LPCSTR Name, LPD3DXFRAME *ppNewFrame );
STDMETHOD( CreateMeshContainer )( THIS_
LPCSTR Name,

[code]....

when the method CreateMeshContainer finished execution, the meshContainer is passed back to its parent like this

*ppMeshContainer = pMeshContainer;

The whole meshContainer stuff is stored persistently inside the frame root wrapped within a class called CMesh So in that process, I haven't initialized pBoneMatrices in anyways. But what and where is the best way to initialize an array of DirectX9 objects.

There is a function called

D3DXMatrixIdentity(&...);

But how can I initialize each one of them with this call?

Notice that pMeshContainer->pBoneMatrices does contain a valid address, despite the fact that the elements inside it are never initialized...

View 5 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++ :: How To Initialize Static Member Of Class With Template And Type Of Nested Class

Oct 7, 2014

How to initialize a static member of a class with template, which type is related to a nested class?

This code works (without nested class):

#include<iostream>
using namespace std;
struct B{
B(){cout<<"here"<<endl;}
};
template<typename Z>

[Code] ,....

View 1 Replies View Related

C :: Initialize Array Values With 0s Or Assume They Will Be Initialized To 0

Apr 7, 2014

The following:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]) {
int i;
int arr[3];

[Code] ....

Notice we didn't set a value for second index but it returns 0. Should I assume that when declaring an array with n values, those values will be initialized to 0 automatically or should I still initialize the array with all 0s doing something like this:

Code:
for(i=0;i<sizeof(arr);i++) {
arr[i]=0;
}

View 11 Replies View Related

C :: Initialize Dynamically Allocated Array Of Integers To Zero?

Jun 22, 2013

Suppose I wished to initialize a dynamically allocated array of integers to zero. Would I do better to use calloc() or malloc + iterate over all entries setting each to zero? Which one is regarded as a better approach?

View 12 Replies View Related

C :: Initialize 1D Or 2D Array In Shared Memory (POSIX)

Dec 8, 2014

I am trying to initialize a 2D char array of strings into POSIX shared memory to be shared between 3 other processes. There are plenty of tutorials on how to use a pointer to share a single string or an integer between processes, but I could find no examples on how to initialize 1D or 2D arrays using mmap(). I have posted what I have so far below. It is the first program, which creates the shared memory object and initialize the array char files[20][2][100] with the value files[0][0][0] = ''. What is the proper method to initialize and share an array in C?

(program initializes shared memory object and array)

Code:
#include<fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/mman.h>

int main (int argc, char *argv[]){

[Code] ......

View 5 Replies View Related

C++ :: Initialize Char Pointer Array In Struct

Nov 28, 2014

I am trying to store data in a struct to be able to read it latter . have problems initializing this.

struct FoodAndDrink {
struct Food {
char* CannedGoods[2] = {
"Canned Spaghetti",
"Canned Tuna",

[code] .....

View 7 Replies View Related

Visual C++ :: Initialize Constructor In Array Of Objects?

Oct 28, 2012

What the best way to initialize a constructor in an array of objects?

Usually I declare an array of objects like so:

Code:
Car toyota[100];

Is this the only way I know to initialize a constructor of an array:

Code:
for (int i = 0; i < 100; i++)
{
toyota[i] = Car(x, y);
}

Is this the proper way?

View 1 Replies View Related

C/C++ :: How To Add Info To Array Of Nested Structures

Dec 4, 2014

I am making a basic music library that stores information rather than files. I need to read from a .txt file the artist's name, the album name, the song name, and the song length. What I am confused about is storing the information in an array of Artist structures.

struct Song{
string songName;
int songLength;
};
struct Album{

[Code] ...

How to read the information into an array of Artists. Also, how would I be able to figure out if the artist already exists in the array when adding a song and if the artist does, add the album under the existing artist.This is how I am reading in the info:

Artist newArtist;
Album newAlbum;
Song newSong;
ifstream inF("library.txt");
while(!inF.eof()) {

[Code] ....

View 1 Replies View Related

C :: Correct Way To Initialize Typedef Structure In Compile Time (with Code)

Mar 6, 2015

Code:
typedef struct token
{
int tokenType; // what token is that
int tokenCode; // the code of a function if applicable
char *tokenString; // Source token
double tokenValue; // if token is a number

[Code] .....

I got several warnings and erros, is it possible to declare a table like that ? What's the correct way to declare it ?

View 4 Replies View Related

C/C++ :: Getting Segmentation Fault While Initializing Nested Structure Variable

Nov 13, 2014

I am not able to assign value into struct addrinfo variable through struct sort_result structure variable.

struct addrinfo {
   int ai_flags; /* Input flags. */  
   int ai_family; /* Protocol family for socket. */  
   int ai_socktype; /* Socket type. */  
   int ai_protocol; /* Protocol for socket. */  

[Code] ....

View 1 Replies View Related

C++ :: Function To Initialize Double Data Array Element To 0.0

Apr 25, 2013

ok here is the question: Write a function that will initialize a double data array element to 0.0 ?

View 4 Replies View Related







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