473,503 Members | 2,107 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

struct redeclare problem

25 New Member
hello,
i have a struct and have declared it as shown below. If i print its value in the main() fn it prints it correctly.However if i declare the struct again in the main() and try to print its value it doesnot give me the correct value but prints some garbage value. Why is it so? Is there any way to correct it because i think i have to declare it again in main() for initializing other variables.
thanks,
prads

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <cstdio>
  6. #include <cstdlib>
  7. using namespace std;
  8. struct set
  9. {
  10.        long int mstoprocess;
  11.        int noofchannels;
  12.        int skipnoofbytes;
  13. }settings={37000,8,0};
  14. int main()
  15. {
  16.     int i;
  17.     static struct set settings;  //if this is removed it works fine!!!!
  18.  cout<<settings.mstoprocess<<endl;
  19. getchar();
  20. return 0;
  21. }
  22.  
  23.  
Nov 4 '07 #1
1 3391
oler1s
671 Recognized Expert Contributor
prads, I’ll answer your question. But I notice that you tend to write with a mix of C and C++ code. Now, it’s fine to use C libraries in C++, but you can’t just use C syntax with C++. One example would be structs, where the C++ syntax differs from C. If you want to use C, use C. If you want to use C++, use C++. The languages are different.

If you are having trouble sticking to one language, it suggests that you don’t have access to a good learning resource. Ideally, you should get a C++ book like C++ Primer by Lippman or Accelerated C++ by Koenig. Those are beginner level books that are also accurate. Most aren’t. If you can’t get either of those books for whatever reason, rely on cprogramming.com or cplusplus.com as they have accurate tutorials. Google for C FAQ and C++ FAQ. Much of the information there is extremely valid as well.

As to your actual question. First, if you have declared and defined struct set before main, then you do not have to declare it afterwards. The declaration is pointless. Hence, your declaration in main is useless.

Second, not only do you declare struct set, but even worse, you create a local variable settings. The settings created in main takes precedence over the global variable settings. However, your local variable settings does not have its member variables initialized to anything. Hence you’ll get “garbage” which in reality is whatever random crud exists in the memory locations for those variables.

The concepts involved in understanding this behavior are: why one initializes variables, and local and global scopes. If you do not understand those topics, you do not have the necessary knowledge to understand the behavior of your code.
Nov 4 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

6
1127
by: KJ | last post by:
Is the following invalid: <d:doc xmlns:d="http://whatever.com/ns"> <d:title xmlns:d="http://whatever.com/ns"> is this invalid? </d:title> </d:doc> Thanks -KJ
16
3809
by: burn | last post by:
Hello, i am writing a program under linux in c and compile my code with make and gcc. Now i have 4 files: init.c/h and packets.c/h. Each header-file contains some: init.h: struct xyz {
3
5311
by: Tee | last post by:
Hi, Anyone know how to redeclare an array with different size but keep the previous data? Just like the redim preserve in VB. Thanks.
11
41051
by: Kimmo Laine | last post by:
I'm flipping my wig here, people. I'm using classes and making each class a file. when I'm including dependet classess, I use require_once to avoid multiple declarations - yet they happen. I put...
1
3235
by: Behzad | last post by:
hi, i want to know what happens to objects in an array if i redeclare the array.. look at the following code: ////my code public MyClass myarray=new MyClass;
2
8090
by: Midgard | last post by:
I have this error: Fatal error: Cannot redeclare send_welcome_mail() (previously declared in /home/oxxowhol/public_html/config.php:21) in /home/oxxowhol/public_html/config.php on line 21 Why?
3
3029
by: Ciegalo | last post by:
Hi to all, I'm getting my hands into PEAR for a small newsletter-sending project. I need to boost the performance of the sending script and came accross this mail_queue class that should queue the...
4
3095
by: mrityunjay11 | last post by:
my ccode goes as such this is php_dbi.php <? function dbi_connect ( $host, $login, $password, $database ) { if ( strcmp ( $GLOBALS, "mysql" ) == 0 ) { $c = mysql_pconnect (...
1
1859
by: srilathaapi | last post by:
Hi All, I am getting the error cannot redeclare the class some xxx in file xxx.php. bcz i m including xxx.php in 3 diffrent functions in the same file to invoke the 3 differnet functions in the...
0
7205
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
7287
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,...
1
7011
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
7468
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...
0
5596
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,...
1
5023
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...
0
4689
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3180
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...
0
3170
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.