473,662 Members | 2,551 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Struct class iteration ...

Hy, I have the fallowing code and I don't understand why is not
working ...

in a header file:

typedef void* cheie;
typedef void* valoare;

struct _Tabel;
typedef _Tabel* Tabel;

struct _Tabel{
cheie valuta[10];
valoare lei[10];
int dim;
};
in a cpp file I have the creating function, and a function for
inserting components. It is all working very fine, except when I want
to type the content of the Tabel struct ...
If I write smt like this:
int i=t->dim; //where t->dim is the number of components
while (i>0)
{
cout<<(char*)t->valuta[i];
i--;
}
the program crash ...
if I do smt like this:
int i=t->dim; //where t->dim is the number of components
int p=0;
t->dim=0;
while (t->dim<i)
{
cout<<(char*)t->valuta[t->dim];
t->dim++;
}
t->dim=i;
it prints only the last value in the vector for t->dim times. Although
very complicated and not elegant, this is the only way I got it to
print smt ...
I don't understand why in the first case the program just crashes and
in the second one is not working properly ... And I can't to things in
other maners because is a school project. If someone can explain me
where I am going wrong, I will be very grateful!
Thanks!

Jul 14 '06 #1
4 1497
Mario wrote:
Hy, I have the fallowing code and I don't understand why is not
working ...

in a header file:

typedef void* cheie;
typedef void* valoare;

struct _Tabel;
An underscore followed by a capital letter (as you do above) means
Undefined Behavior.
typedef _Tabel* Tabel;

struct _Tabel{
cheie valuta[10];
valoare lei[10];
int dim;
};
in a cpp file I have the creating function, and a function for
inserting components. It is all working very fine, except when I want
to type the content of the Tabel struct ...
If I write smt like this:
int i=t->dim; //where t->dim is the number of components
while (i>0)
{
cout<<(char*)t->valuta[i];
C-style casts (like you do above) and casting to and from void* is
generally a bad idea, because it often masks underlying problems.
i--;
}
the program crash ...
if I do smt like this:
int i=t->dim; //where t->dim is the number of components
int p=0;
t->dim=0;
while (t->dim<i)
{
cout<<(char*)t->valuta[t->dim];
t->dim++;
}
t->dim=i;
it prints only the last value in the vector for t->dim times. Although
very complicated and not elegant, this is the only way I got it to
print smt ...
I don't understand why in the first case the program just crashes and
in the second one is not working properly ... And I can't to things in
other maners because is a school project. If someone can explain me
where I am going wrong, I will be very grateful!
This is answered in the FAQ:

http://www.parashift.com/c++-faq-lit...t.html#faq-5.8

Good luck.

Best regards,

Tom

Jul 14 '06 #2
typedef void* cheie;

make this:

typedef char * cheie;
cout<<(char*)t->valuta[i];
and make this:

cout << t->valuta[i];

There is code that you haven't shown us. Somewhere you are writing lines
like:

t->valuta[i] = SOMETHING;

If something is not a char *, then the code you wrote isn't going to
work. Changing the typedef of cheie to char * will make the mistake obvious.
Jul 14 '06 #3

Howard Gardner wrote:
typedef void* cheie;

make this:

typedef char * cheie;
cout<<(char*)t->valuta[i];

and make this:

cout << t->valuta[i];

There is code that you haven't shown us. Somewhere you are writing lines
like:

t->valuta[i] = SOMETHING;

If something is not a char *, then the code you wrote isn't going to
work. Changing the typedef of cheie to char * will make the mistake obvious.
No, I do all the necessary casts, remember I sad that the creating and
the inserting functions ar going allright. The only problem is that I
can't figure why you can't just write
t->valuta[i] ???
if i write the value of t->dim, before I write lets say t->dim=123, it
does prints 123, but the printed value of t->valuta[t->dim] is the
last value inserted ... and a didn't find the logic in all this ... So
.... I hope for other ideas ...

Jul 14 '06 #4
Mario wrote:
Hy, I have the fallowing code and I don't understand why is not
working ...

in a header file:

typedef void* cheie;
typedef void* valoare;

struct _Tabel;
typedef _Tabel* Tabel;

struct _Tabel{
cheie valuta[10];
valoare lei[10];
int dim;
};
in a cpp file I have the creating function, and a function for
inserting components. It is all working very fine, except when I want
to type the content of the Tabel struct ...
If I write smt like this:
int i=t->dim; //where t->dim is the number of components
while (i>0)
{
cout<<(char*)t->valuta[i];
i--;
}
When t->dim is the number of components, they would ordinarily be
located in valuta[0] through valuta[t->dim - 1]. Move the i-- up above
the cout statement to get the indexing right. If you've used similar
code to set the values, chances are that somewhere the code set the
value of valuta[10], which isn't valid. Array indexes run from 0 to one
less than the size of the array.
the program crash ...
if I do smt like this:
int i=t->dim; //where t->dim is the number of components
int p=0;
t->dim=0;
while (t->dim<i)
{
cout<<(char*)t->valuta[t->dim];
t->dim++;
}
t->dim=i;
it prints only the last value in the vector for t->dim times. Although
very complicated and not elegant, this is the only way I got it to
print smt ...
I don't understand why in the first case the program just crashes and
in the second one is not working properly ... And I can't to things in
other maners because is a school project. If someone can explain me
where I am going wrong, I will be very grateful!
Thanks!

int i = 0;
while (i < t->dim)
{
cout << (char*)t->valuta[i] << '\n';
++i;
}

That does the same thing as the preceding code snippet, but much more
clearly. It doesn't explain why your code prints only the last value,
multiple times. But I suspect that the actual code was different from
this code.
Jul 14 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1419
by: Stefan Behnel | last post by:
Hi! I filed a patch for a Heap class to be integrated into the heapq module (in addition the the currently available functions). The main features are support for iteration and for the standard keyword arguments of sort() and sorted(): key, cmp, reverse. http://sourceforge.net/tracker/index.php?func=detail&aid=1162363&group_id=5470&atid=305470 I wrote it because I believe that it makes the API of that module much simpler
21
4637
by: Kilana | last post by:
I see this all the time in code: typedef struct a_struct { ... }differentName, *differentNamePtr; I understand how I can use it, but could someone tell me why the above is
2
11242
by: SACHIN | last post by:
I have this class as part of a Consol application. using System; namespace Bugreport { /// <summary> /// This class tries to use the Class/Struct combination. /// </summary> class Class1 {
4
31571
by: Steve | last post by:
I'll be the first to admit, I'm not entirely clear on the appropriate usage of either. From what I am reading in my books, a Struct and a Class are pretty much the same, with the difference being, a Class can have private and protected members, but a Struct everything is Public by default. I laymans terms what would be an appropriate reason to choose a Struct over a Class? So why would one want to choose a Class over a Struct.
15
9053
by: Steven T. Hatton | last post by:
The following may strike many of you as just plain silly, but it represents the kind of delelima I find myself in when trying to make a design decision. This really is a toy project written for the purpose of learning to work with C++. It therefore makes some sense for me to give the situation the amount of consideration presented below. To be quite honest, I'm amazed at the amount there is to say about such a seemingly simple...
8
1592
by: J.Marsch | last post by:
Ok, I think that what I'm seeing is due to some implicit boxing/unboxing going on, but I'd like to understand this a little better. I have a collection of structs. If I try to iterate the collection of structs with a foreach() loop, and in the loop I try to change the value of one of the fields in the struct, I get a compile time error "The left-hand side of an assignment must be a variable, property, or indexer". (a very contrived code...
7
2324
by: Anders Borum | last post by:
Hello! As part of a refactoring iteration, I was looking at consolidating two properties (Start and EndDate) to a single structure that would allow a single access point for the "duration" (so to speak). I considered using the TimeSpan, but that structure doesn't provide the boundaries for the "duration", only the length of the "duration" itself. The obvious step next would be creating my own structure that provided these
12
2179
by: mast2as | last post by:
Hi everyone I am working on some code that uses colors. Until recently this code used colors represented a tree floats (RGB format) but recently changed so colors are now defined as spectrum. The size of the vector went from 3 (RGB) to 151 (400 nm to 700 with a sample every 2nm). The variables are using a simple Vector class defined as follow: template<typename T, int Depth> class Vector
2
2298
by: Ninereeds | last post by:
I'm messing around with using mixin-layers (look for papers by Yannis Smaragdakis and Don Batory) to define data structures. One issue is that nodes tend to have pointers to other nodes - the pointers have to point to the full node type, and have to be referenced before that full node type is known. One solution is to use the 'fixpoint construction' to get an apparent circular dependency... class c_Final : public c_Layer2< c_Layer1...
0
8432
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8344
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8857
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8633
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7367
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6186
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4180
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4347
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1752
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.