C/C++ :: Accessing Elements Of Struct Array
Dec 9, 2014
I've been working for some number of days on this code to take information about movies from both a file and the user, store the infos in an array of structs, and at the end, write all the info out to a file. I'm having some problems with an error message reading:
"prog.c:102:11: error: subscripted value is neither array nor pointer nor vector"
this error occurs in many lines (which I will label specifically below -- they are everywhere where I am trying to access/modify an individual element of a struct element of the array).
A few examples of where I am having the problems are lines:
39, 52-55, 70, 72, and 86 (and more of the same exact variety).
I am obviously rather systematically doing something wrong, but I am quite certain all of these are the exact same mistakes.
I pull up also 2 or 3 other errors, but I don't think they are related and should be able to fix them quickly once I work out this conundrum.
#include <string.h>
#include<stdlib.h>
#include <stdio.h>
int moviecount =0;
typedef struct{
int year;
[Code] .....
View 5 Replies
ADVERTISEMENT
Jul 26, 2014
I want to access the elements of my array dynamically. So far I've only figured out how to do this manually. if I tried it like this my code would work but there should be a better way right?
View 10 Replies
View Related
Jan 16, 2015
I am trying to retrieve the elements in an array to print them. I am pretty sure I have everything right, but it is not working. There are 5 values in the array {3, 4, 10, 5, 6}, but the first two elements (supposed to be 3,4) are coming out a 0,-2654238590. The last three elements are coming out correct.
void addDynArr(DynArr *v, TYPE val) {
/* FIXME: You will write this function */
assert(v != 0);
//resize if necessary
if(v->size >= v->capacity)
[Code] ....
View 6 Replies
View Related
Mar 31, 2013
Code:
int arr[10];
int* p = arr;
int (*p2)[10] = &arr;
So, pointer p is a pointer to an array, I can use it to access elements of arr as in *(p+5).
Pointer p2 is a pointer to an array of ten integers. What is it for, how can I use it to access elements of arr?
View 4 Replies
View Related
Feb 13, 2015
How can I display the elements of an array inside struct something like this
struct ABC {
double a[3];}
void show( const ABC & x );
ABC x{18, 'W', { 1.1, 2.2, 3.3 }};
show(x); would output {18, ‘W’, {1.1, 2.2, 3.3}}
View 10 Replies
View Related
Dec 1, 2014
Let's say there is a document which stores data of exams of 3 subject. The document is in the below format:
Subject code [spc] Student code [spc] Exam score [endl]
Repeatedly, there are 100 data. E.g.
ENGL_S12 [spc] 000001 [spc] 90.5
ENGL_S12 [spc] 000005 [spc] 77.3
MATH_G22 [spc] 000502 [spc] 100
LATI_F11 [spc] 002005 [spc] 65.4
...
Now I have to write a function show_exam_descending(Data d, string subCode)
when I call show_exam_descending(d, "ENGL_S12")
the program will execute to show all the students' exam scores in ENGL_S12 in DESCENDING order...
For this to run, I have declared a struct Data:
struct Data {
string subjectCode;
int studentCode;
double examScore;
);
For the search, I have written a function before to load all the data from the document by using pointer and dynamic arrays. It works so well. What troubles me is the way to swap the elements (i.e. examScore) of different students in struct dynamic arrays. I am able to display all of them, but don't know how to swap.
View 8 Replies
View Related
Feb 9, 2014
I'm confused about accessing elements from arrays. Supposed I have an array of pointers to char:
char *names = { "John", "Rose", "Steven" };
To access one of the strings, should I use names[ 0 ][ i ], where i is an index in the set ( 0, 1, 2 ), or should I use names[ i ]? I would think it would be the first option, because this array has 1 dimension that contains others arrays, right?
View 8 Replies
View Related
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
May 1, 2015
so lets assume i have a nested vector in a set or vice versa o in a more general form a container in a container example...
Code:
std::set<vector<int> > my_tuple;
How do i access individual elements of lets say the first vector in the set! Or the last element in the 3rd vector?
View 7 Replies
View Related
Mar 24, 2013
I have a queue of structs. I would like to access one of the two pieces of data at the front of the queue that each struct maintains.
Here is some of my code:
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <queue>
using namespace std;
[Code] ....
What I am asking is how do I get the priority of the struct item at the front (lane1.front()), in this case?
View 8 Replies
View Related
Mar 18, 2014
The program I'm working on is a very basic relational database. I've isolated my problem for simplicity. I get a segfault right here when I try to access db->relationCount. I tried printing db->relationCount from within loadDB and that worked,
[code]
loadDB(db, configFile);
printf("%d",db->relationCount);
fflush(stdout);
View 5 Replies
View Related
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
Apr 30, 2013
I have a vector (structures) in a struct (instances). I make a declaration of this struct called instance. The vector is a 3-layer vector of pointers, like so:
vector < vector < vector<scene::IAnimatedMeshSceneNode*> > > structures; (The type is from Irrlicht 3D). I have 3 nested "for" loops which looks similar to the following:
for (int a = 0; a < instance.structures.size(); a++) { /*note:vector size previously set*/
for (int b = 0; b < instance.structures[a].size(); b++){
for (int c = 0; c < instance.structures[a][b].size(); c++) {
if (1) { //checking value of variable not included in snippet
(instance.structures)[a][b][c] = smgr->addAnimatedMeshSceneNode(fl);
(instance.structures)[a][b][c]->setPosition(renderPos);
}
}
}
}
The problem is in these two lines, I think:
(instance.structures)[a][b][c] = smgr->addAnimatedMeshSceneNode(fl);
(instance.structures)[a][b][c]->setPosition(renderPos);
These are currently referencing the pointers, it seems. The program compiles but crashes at this point. I need them to reference the values of the pointers. Problem is, I don't know where to put the dereference operator (*). Where should it go?
View 4 Replies
View Related
Apr 5, 2013
I currently have a file which allows inputs to record different transistor types. I then have the task of accessing this structure, find a certain manufacturer ID, and print the information about this particular transistor.
My problem is accessing the array to search through.
Here is my code:
Code:
#include "stdio.h"
const int IDLEN=30; //All constant values defined
const int POLARITYLEN=3;
const int MAXSTOCKITEMS=10;
//First structure defined
struct TransistorRec {
[Code]......
The errors I am currently getting are on line 54 'expected primary-expression before "struct"' and on line 60 ' 'maunfacturersID' undeclared'
View 11 Replies
View Related
Feb 29, 2012
I'm currently working on the ioquake3 engine . The ioquake3 engine is separated into 2 different main threads at runtime: the gamecode and the engine. Both are communicating but not all information and my problem resides here.
In the gamecode, there's a struct called gentity_t which contains a lot of fields:
Code:
typedef struct gentity_s gentity_t;
struct gentity_s {
entityState_ts;// communicated by server to clients
entityShared_tr;// shared by both the server system and game
// DO NOT MODIFY ANYTHING ABOVE THIS, THE SERVER
// EXPECTS THE FIELDS IN THAT ORDER!
//================================
struct gclient_s*client;// NULL if not a client
[Code] ....
This whole entity is passed to the engine at runtime, but only the first two fields are declared for the engine:
Code:
// the server looks at a sharedEntity, which is the start of the game's gentity_t structure
typedef struct {
entityState_ts;// communicated by server to clients
entityShared_tr;// shared by both the server system and game
} sharedEntity_t;
My problem is that I need to access the health field of gentity_t from the engine. Technically, this is possible, but the health field is not declared in sharedEntity (which is the same memory address than gentity_t in the gamecode), so this is not straightforward.
I am looking for an elegant way to do this, and my constraint is that I must not edit the gamecode, only the engine.
The solutions I've thought:
- Just copy the whole gentity_t fields into sharedEntity_t. This would work I think but would be redundant, and I would like to avoid copying this huge set of fields.
- Include the two headers files declaring the gentity_t and sharedEntity_t structs, and create a Getter and a Setter functions that would cast a gentity_t over a sharedEntity_t and return/set a field. The problem is that I can't simply include them because they are both including some common headers files and this produce a recursive include error (and I can't modify the files to add a check, these are normally in the gamecode).
- Directly access the health field using a clever memory pointer, but I don't even know if that's possible given the huge number of fields prior health with many different types?
View 2 Replies
View Related
Jan 4, 2014
I am parsing a binary data file by casting a buffer to a struct. It seems to work really well apart from this one double which is always being accessed two bytes off, despite being in the correct place.
Code:
typedef struct InvoiceRow {
uint INVOICE_NUMBER;
...
double GROSS;
...
char VAT_NUMBER[31];
} InvoiceRow;
If I attempt to print GROSS using printf("%f", row->GROSS) I get 0.0000. However, if I change the type of GROSS to char[8] and then use the following code, I am presented with the correct number...
Code:
typedef struct example {
double d;
}
example;
example *blah = (example*)row->GROSS;
printf("%f", blah->d);
View 7 Replies
View Related
Aug 27, 2014
Write a program that reads in the names and the ages of ten people. Store these data in two arrays (make sure that the entered names are not longer than the array size you choose). Then produce a table of ten lines, with each line giving the name and age of a person along with the (positive or negative) deviation of that person's age from the average age. The code I wrote to fill in the arrays looks as follows
Code:
#include<stdlib.h>
#include<stdio.h>
int main()
{ // begin main()
// array length
[Code]....
When I compile it, it gives me warnings that few of the variables I declared aren't used, but that is not affecting the program at all. When I run the program, it allows me to pass the names and ages into the arrays, but when it comes to displaying them, I'm getting a 'Segmentation fault'. I used very similar code in Java to write the program, and it worked fine so what is the problem with C then?
View 8 Replies
View Related
Nov 22, 2012
I was wondering if it was possible to access data from an array inside a threaded function after it has finished executing?
Take this example:
Code:
#include <iostream>
#include <boost/thread.hpp>
using namespace std;
[Code]....
After workerFunc has finished, how could I access the array arr? Is there any way to get (3,4,5,6,7) back into main()?
View 5 Replies
View Related
Mar 7, 2013
I am writing a C program to access a string into an array from a pointer array in Visual Studio 2010. Program is given below:-
Code:
#include <stdio.h>
#include <string.h>
void main() {
char data_a[6],data_b[6]="ABcde",*ptr;
ptr=&data_b[0];
data_a[0]=*ptr;
printf("%s",a);
}
I am getting some junk character first and then my actual data on console window. I want to where I am getting wrong and how to resolve it.
View 4 Replies
View Related
Feb 8, 2014
I'm okay with 2-dimensional arrays, but when I get to 3 or more, I can't seem to wrap my head around how to assign and/or pull values from specific parts.
To give an example, let's take the following example:
We know that a player can take up to 5 total quests, and each quest can have a max of 5 tasks.
Let's assume I have the following multi-dimensional array holding all of a players quest data:
quests[0] = 78;// Store the questID
quests[0][0] = 3945;// Store the 1st creature ID
quests[0][1] = 2230;// Store the 2nd creature ID
quests[0][2] = 3045;// Store the 3rd creature ID
quests[0][0][0] = 2;// Store how many needed of the 1st creature
quests[0][1][0] = 5;// Store how many needed of the 2nd creature
quests[0][2][0] = 13;// Store how many needed of the 3rd creature
As we know, the above code can't be done. How do I assign certain values to each specific dimension?
View 6 Replies
View Related
Aug 4, 2014
I have a contiguous sequence of section headers in a file (all the data in the file is stored in void * data), where each section header is the same size and has the same fields in the same order. So it's laid out like an array.
I have to:
- Use an offset value and the number of section headers variables to identify the location and length of the section header table (these are hdr.offset and hdr.length respectively).
- The offset value shows the distance between the start of the file and the start of the first section header (so I need a pointer to the start of the first section header)
- Apply a typecast to location of the section header table to process it as an array of section headers.
Here is what I have done, but I am getting a segmentation fault. What am I doing wrong? How do I fix this?
typedef struct {
unsigned int name;
unsigned int type;
} SectionHeader;
[Code] ....
View 1 Replies
View Related
Jan 10, 2015
Write a program using user-defined function which accepts an integer array and its size as arguments and assign the elements into a two dimensional array of integers in the following format: If the array is 1,2,3,4,5,6, the resultant 2D array is
1 2 3 4 5 6
1 2 3 4 5 0
1 2 3 4 0 0
1 2 3 0 0 0
1 2 0 0 0 0
1 0 0 0 0 0
View 1 Replies
View Related
Jan 21, 2013
Write a function that takes an array and returns true if all the elements in the array are positive, otherwise, it returns false.
View 6 Replies
View Related
Sep 27, 2013
I am having problem in writing the code for the problem "To assign the elements of 1-D integer array into 2-D array of integers such as if the array is 1,2,3,4,5,6 The resultant 2-D array should be like :
1 0 0 0 0 0
1 2 0 0 0 0
1 2 3 0 0 0
1 2 3 4 0 0
1 2 3 4 5 0
1 2 3 4 5 6
"
View 7 Replies
View Related
May 28, 2013
I wrote this simplified version of a program i am writing that parses data in UDP packets. In the process of doing so i pretty much answered all my questions and fix all the problems i was having.
decodeSystemMap function will be in loop, and will proccess packets that have mostly the same data, only a few items will be added or changed or deleted.
whats the best way to check if there are any new, deleted, or removed items in the packet and only modify those?
Is there anything unsafe / dangrous about the way the code is now?
Code:
/* * File: main.c
* Author: david
*
* Created on May 23, 2013, 11:57 AM
*/
#include <stdio.h>
#include <stdlib.h>
[Code] ....
View 4 Replies
View Related
Aug 13, 2014
I have just started learning C language, I have read that array name is the pointer to the first element of array.
So,technically both a and a[0] should have different memory address and a should hold address of a[0],
But when I declare an array and print the address:
int a[5]={2,4,6,8,10};
printf("The address of a and a[0] are &d and %d",&a,&a[0]);
Output is: The address of a and a[0] are 2358832 and 2358832
Why I am getting the same result, shouldn't I be getting different memory address for both a and a[0].
View 2 Replies
View Related