473,395 Members | 1,496 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

problem with array of struct

Hi!
I've the follwoing in my C/C++-Program:

There's a struct defined as following:

struct t_datensatz {
char flag;
char ID;
char Tel[29];
};

..
..

int main void() {
t_datensatz ds[2];

ds[0].flag='1';
ds[0].ID='2';
strcpy(ds[0].Tel,"TelegrammmmmmmmmmmmmmmmmmmmmA");
cout << "OUT: " << ds[0].flag << ds[0].ID << ds[0].Tel << endl;

ds[1].flag='3';
ds[1].ID='4';
strcpy(ds[1].Tel,"TelegrammmmmmmmmmmmmmmmmmmmmB");
cout << "OUT: " << ds[1].flag << ds[1].ID << ds[1].Tel << endl;

ds[2].flag='5';
ds[2].ID='6';
strcpy(ds[2].Tel,"TelegrammmmmmmmmmmmmmmmmmmmmC");
cout << "OUT: " << ds[2].flag << ds[2].ID << ds[2].Tel << endl;

// This doesn't work, why??
for(int i=0;i<=2;i++)
cout << "OUT: " << ds[i].flag << ds[i].ID << " " <<
ds[i].Tel <<
endl;

}

Produced Output:
OUT: 12TelegrammmmmmmmmmmmmmmmmmmmmA
OUT: 34TelegrammmmmmmmmmmmmmmmmmmmmB
OUT: 56TelegrammmmmmmmmmmmmmmmmmmmmC
OUT: 12 TelegrammmmmmmmmmmmmmmmmmmmmA34Telegrammmmmmmmmmmm mmmmmmmmmB56TelegrammmmmmmmmmmmmmmmmmmmmC
OUT: 34 TelegrammmmmmmmmmmmmmmmmmmmmB56Telegrammmmmmmmmmmm mmmmmmmmmC
OUT: 56 TelegrammmmmmmmmmmmmmmmmmmmmC


Ok, the first three lines are fine. But why does the for-statement not
work as it is intented to??? It should produce exactly the same output
as the "manual" cout-statements.
I guess the problem is "ds[i].Tel". What's wrong in that little
program?

Tnx,
Markus
Jul 19 '05 #1
3 10795
Markus wrote:
Hi!
I've the follwoing in my C/C++-Program:

There's a struct defined as following:

struct t_datensatz {
char flag;
char ID;
char Tel[29];
};

.
.

int main void() {
t_datensatz ds[2];
Holds 2 items.
ds[0].flag='1';
ds[0].ID='2';
strcpy(ds[0].Tel,"TelegrammmmmmmmmmmmmmmmmmmmmA");
cout << "OUT: " << ds[0].flag << ds[0].ID << ds[0].Tel << endl;

ds[1].flag='3';
ds[1].ID='4';
strcpy(ds[1].Tel,"TelegrammmmmmmmmmmmmmmmmmmmmB");
cout << "OUT: " << ds[1].flag << ds[1].ID << ds[1].Tel << endl;

The following results in overflow of ds. ds[2].flag='5';
ds[2].ID='6';
strcpy(ds[2].Tel,"TelegrammmmmmmmmmmmmmmmmmmmmC");
cout << "OUT: " << ds[2].flag << ds[2].ID << ds[2].Tel << endl;

// This doesn't work, why??
for(int i=0;i<=2;i++)
cout << "OUT: " << ds[i].flag << ds[i].ID << " " <<
ds[i].Tel <<
endl;
Your for loop is of course also doing 3 instead of 2...overflow...
}

Produced Output:
OUT: 12TelegrammmmmmmmmmmmmmmmmmmmmA
OUT: 34TelegrammmmmmmmmmmmmmmmmmmmmB
OUT: 56TelegrammmmmmmmmmmmmmmmmmmmmC
OUT: 12 TelegrammmmmmmmmmmmmmmmmmmmmA34Telegrammmmmmmmmmmm mmmmmmmmmB56TelegrammmmmmmmmmmmmmmmmmmmmC
OUT: 34 TelegrammmmmmmmmmmmmmmmmmmmmB56Telegrammmmmmmmmmmm mmmmmmmmmC
OUT: 56 TelegrammmmmmmmmmmmmmmmmmmmmC

That is odd results, but the output of your program is undefined so...

Ok, the first three lines are fine. But why does the for-statement not
work as it is intented to??? It should produce exactly the same output
as the "manual" cout-statements.
I guess the problem is "ds[i].Tel". What's wrong in that little
program?
buffer overflow at the least. I don't see anything else wrong.
Tnx,
Markus

--
Noah Roberts
- "If you are not outraged, you are not paying attention."

Jul 19 '05 #2
Markus wrote:
Hi!
I've the follwoing in my C/C++-Program:

There's a struct defined as following:

struct t_datensatz {
char flag;
char ID;
char Tel[29];
};

.
.

int main void() {
The above makes me suspicious. Is that really the exact code you tried?
t_datensatz ds[2];

ds[0].flag='1';
ds[0].ID='2';
strcpy(ds[0].Tel,"TelegrammmmmmmmmmmmmmmmmmmmmA");
You're writing one beyond the end of ds[0].Tel. It can hold 29 elements,
but you're feeding 30 to it. Don't forget the '\0' character that is
automatically appended to string literals.
cout << "OUT: " << ds[0].flag << ds[0].ID << ds[0].Tel << endl;

ds[1].flag='3';
ds[1].ID='4';
strcpy(ds[1].Tel,"TelegrammmmmmmmmmmmmmmmmmmmmB");
cout << "OUT: " << ds[1].flag << ds[1].ID << ds[1].Tel << endl;

ds[2].flag='5';
ds[2].ID='6';
strcpy(ds[2].Tel,"TelegrammmmmmmmmmmmmmmmmmmmmC");
cout << "OUT: " << ds[2].flag << ds[2].ID << ds[2].Tel << endl;
Again, you're writing one beyond an array, this time it's the array ds,
which can hold 2 elements, but you try to write 3 of them into it.
// This doesn't work, why??
for(int i=0;i<=2;i++) ^^
Always think twice if the end condition for the loop counter contains a
<=. Your array only can hold 2 elements, from ds[0] to ds[1], so your
loop counter must stop before 2, i.e. it must be 'i<2'.
cout << "OUT: " << ds[i].flag << ds[i].ID << " " <<
ds[i].Tel <<
endl;

}


Jul 19 '05 #3
Markus wrote:

Hi!
I've the follwoing in my C/C++-Program:

There's a struct defined as following:

struct t_datensatz {
char flag;
char ID;
char Tel[29];
};


Why are you using char buffers instead of std::string? Especially fixed
width buffers, which often waste space and are subject to buffer
overflow. As others have pointed out, that's what your problem is.


Brian Rodenborn
Jul 19 '05 #4

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

Similar topics

2
by: Angelo Secchi | last post by:
I'm trying to use the unpack method in the struct module to parse a binary file without success. I have a binary file with records that include many fields for a total length of 1970. Few days ago...
6
by: The_Kingpin | last post by:
Hi again guys, I've decided to cut my project in section and I found it way easier like this. I'm having a little problem reading struct in a file though. I think after this I'll be able to...
2
by: coinjo | last post by:
How to load components of a user defined struct from a file int an array?
4
by: hobbes992 | last post by:
Howdy folks, I've been working on a c project, compiling using gcc, and I've reached a problem. The assignment requires creation of a two-level directory file system. No files have to be added or...
3
by: langog.info | last post by:
hi: in c,i use : int a,b={1,2,3,4,5}; struct { int c; }aaa,bbb; bbb.c=bbb.c=bbb.c=bbb.c=bbb.c=2;
19
by: rmr531 | last post by:
First of all I am very new to c++ so please bear with me. I am trying to create a program that keeps an inventory of items. I am trying to use a struct to store a product name, purchase price,...
2
by: quadraticformula | last post by:
Hey, quick question for anyone willing to listen. I've always wondered why I can initialize an array of structs with something like this (where "..." represents the 14 separate values for the...
1
by: Charming12 | last post by:
Hi All, The question is regarding Unmanaged and Managed code conversion in C# for structures. I have two structures like: public struct Detail { public int age; public string address;
1
by: elke | last post by:
Hi, I want to use an unmanaged dll in C# .net and I'm having some troubles witch a function that should return an array. I'm new at this, so I don't know what I'm doing wrong. Here is some...
1
by: lye85 | last post by:
#include <stdio.h> #include <stdlib.h> #include <string.h> struct account { char AccName; int Age; double AccBalance; struct account *Next;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.