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
ADVERTISEMENT
Aug 7, 2013
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 6 Replies
View Related
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
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
View Related
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
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
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
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
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
Feb 26, 2015
If I have an integer variable like int a=9 then in the switch case If i write :
switch(a) {
case 4+a: printf("hii");
}
Then why is this statement a compile-time error that variables cannot be used inside a case statement why does the compiler not subtitutes the values in place of the variables.
View 1 Replies
View Related
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
Apr 24, 2013
I'm about to begin work on an exercise that requires me to maintain a list of the variables inside a c source file. I'm trying to figure out how I'm going to pluck out the variable names from all the other noise. My first thought would be to count any word that isn't a keyword or other type of grammar syntax, such as parenthesis, brackets, semicolons, etc. right?
View 10 Replies
View Related
Feb 22, 2013
I have two classes, a Package class and a Person class. The Package class has two Person objects has member variables, a Sender and a Receiver. While overloading the << operator for the Package class so that it will make an output label from everything in the Package class. Here is my code...
class Package{
public:
Person Sender;
Person Reciever;
int weight;
double cost;
friend ostream &operator<<(ostream &out, Package &pack);
[Code] .....
So my problem is on that last output line, I am unable to call Sender.getName()... etc. Is there a proper syntax so that I can access the members of the Person class while overloading the << operator for the Package class?
View 2 Replies
View Related
Oct 10, 2014
I have an array of structure that takes family information (name, age, and state) after it takes the users input it prints back all of the information that was input and then prints the family members that just live in Texas. After that I am trying to implement a function to average out the ages of all family members but I cant seem to get it right, whenever it gets to that part of the program it just outputs 0. I also tried adding an & sign prior to FAMILY[i].age and got an error of int from ptr with no cast.
Also I realize that the code only shows adding of the family members ages which is fine for now as I am concentrated on getting that to work in the first place.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//Global Variables
int i=0, a=0, sum=0;
[Code].....
View 8 Replies
View Related
Jul 13, 2014
I have a project for class where I have to create a structure and get user input for 3 structure variable arrays of 10. I am trying to figure out how I can use the same function to fill my different section of variables.
My Structure is an employee file of ID number, name, hours, payrate, and then gross pay. I have to create a function for each input function. I am confused on how to pass the structure variable so that I do not have to write 3 functions for each input. I would like to be able to get all the info for the first structure variable and then recall the same 5 functions for the next before moving along. I hope that I have been able to make this clear. Here is my code:
#include<iostream>
#include<iomanip>
#include<cctype>
#include<string>
#include<cstring>
using namespace std;
struct PayPeeps_CL//Payroll Structure {
[Code] .....
View 14 Replies
View Related
Jan 8, 2015
#include <iostream>
#include <string>
struct WeatherStats {
double total_rainfall;
int high_temp;
int low_temp;
int avg_temp;
[Code] ....
When I run this program I am able to input data for the three months but after inputting everything I am prompted with a run time error that states: Run time check failure#3: The variable 'temp' is being used without being initialized. I've underlined the statement that the compiler says is causing this error, yet there is no variable 'temp' in that statement.
Computer are very specific right? So in the problem statement total_yearly_temp = total_yearly_temp + temp.avg_temp; there is a variable called total_yearly_temp and one called temp.avg_temp, but there are none called temp. It can't be complaining about the WeatherStats variable I defined in the first line of the function called temp because I did the exact same thing in the previous function and there are no errors concerning that.
View 3 Replies
View Related
Sep 7, 2014
I keep getting the "Uninitialized Local Variable" error. But for my code it's says it's the variable 'pay' in my Manager Function. This is the only error that is popping up.
I've tried setting pay to 0 but when I do, I get a bunch of external errors. I've also tried assigning pay to WeeklySalary like this:
double pay = WeeklySalary;
//Calculating pay for a company
#include <iostream>
#include <iomanip>
using namespace std;
//Function prototypes
double managerFunction();
double hourlyWorkerFunction();
double commissionWorkerFunction();
[Code] .....
View 10 Replies
View Related
Nov 24, 2014
I am getting this message from Valgrind, As far as I can see what it points to is initialized. The memory it is referring to is freed in unload and I was not getting this error until after I added the check function. Valgrind was happy. Here is the code and the error message from Valgrind. I am trying to create a spell checker for an assignment for a online class I am taking. I just want to get this table working correctly before I add it to the rest of the program. The code seems to run fine but I have come to see that dos not mean much in C.
#include "hashTable.h"
#include <stdbool.h>
table_node hash_table[TABLE_LENGTH];
bool loaded = true;
//assigning letters a-z to table nodes for buckets
void key_hash_t( )
[Code] ....
Valgrind error:
arortell@gentoo ~/Development/Projects/C_Projects/Data_Structures/HashTable $ valgrind --leak-check=full --track-origins=yes ./hashTable
==11360== Memcheck, a memory error detector
==11360== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==11360== Using Valgrind-3.9.0 and LibVEX; rerun with -h for copyright info
==11360== Command: ./hashTable
[Code] ....
View 10 Replies
View Related
Jan 10, 2013
I have airport class which should navigate planes, in its list to runways, with method move, theres a method prepare which changes the direction of flight to all planes, always before move is called, move just increments decrement x and y of plane in its list. But after calling two times in row airport->move(), I get screwed and I really dont know wheres the problem. Have I badly initiazed something? Iterator gets invalidated.
Valgrind Stacktrace
Conditional jump or move depends on uninitialised value(s)
==26207== at 0x409601: plane::move() (in /home/xnovak11/Downloads/airport/main)
==26207== by 0x401FBD: airport::move() (in /home/xnovak11/Downloads/airport/main)
==26207== by 0x405FE1: io::start(std::istream&, std:stream&, std:stream&) (in /home/xnovak11/Downloads/airport/main)
This is how I add planes into list in airport
Code:
list<plane*> planes;
list<plane*>::const_iterator planeIterator;
list<plane*>::iterator planeIteratorMoj;
bool airport::addPlane(const plane& p) {
[Code] .....
This is the method where it fails. When I call it once, no problem, after second call I get instead of normal number in cout<<after move<< s1 i get like 8795456 ....
Code:
void airport::move() {
for(planeIteratorMoj = planes.begin(); planeIteratorMoj!= planes.end(); planeIteratorMoj++) {
plane * p1 = (*planeIteratorMoj);
int s,w;
p1->getPosition(s,w);
[Code] .....
View 3 Replies
View Related
Jul 13, 2012
I had a flawed function like this:
Code:
fn(){
char c;
if (runFirstTime){
#ifdef VC
c='';
#else
c='/';
#endif
}
... // c is used in the rest of the function to construct some pathnames
}
The problem is that the value of c is not defined the 2nd time the function is called (and subsequently). It somehow worked fine under CygWin compiled with gcc. So I didn't spot the flaw until it ran incorrectly under Windows complied with VC++ 2010. Then I found the error and changed the code to something like
Code:
fn(){
#ifdef VC
const char c='';
#else
const char c='/';
#endif
...
}
So now it works correctly under Windows. Then I re-compiled the new code with gcc and to my surprise gcc produced exactly the same binary! How can this be? Does the gcc compiler see my flaw and fix it for me somehow?
View 14 Replies
View Related
Aug 2, 2014
I discovered valgrind and started using it for my c code. But I get following error message at almost every malloc position, :
==19505== 40 errors in context 10 of 12: ==19505== Use of uninitialised value of size 8 ==19505== at 0x10000416E: my_method (main.c:662) ==19505== by 0x10000159E: main (main.c:182) ==19505== Uninitialised value was created by a heap allocation ==19505== at 0x47F1: malloc (vg_replace_malloc.c:302) ==19505== by 0x100001C21: my_method (main.c:333) ==19505== by 0x10000159E: main (main.c:182)
and I really don't understand what it means. I already googled it but I didn't find out what is my mistake.SO here i just put one example:
Code:
int main(int argc, char** argv) {
//i declare my variables at this position
Uint *used, *forbidden_jumps, *forbidden_jumpsV,
*forbidden_jump;
/*now i want to allocate one of them, this is my line 333 from the error message*/
//a_num is set during the execution of the program,
ALLOC(used, Uint, a_num);
}
[code].....
Is there any support page for the output of valgrind? I found it on the homepage.
View 8 Replies
View Related
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
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
Mar 27, 2015
In Visual Studios I keep getting this error. cpp(36): error C4700: uninitialized local variable 'Eligible' used
Code:
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <cmath>
using namespace std;
void Getinput(int& Loantype, double& Income, double& Totaldebt, double& Loanamount);
[Code] ....
View 4 Replies
View Related
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
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