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.Howev er 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 -
#include <iostream>
-
#include <fstream>
-
#include <cstring>
-
#include <cmath>
-
#include <cstdio>
-
#include <cstdlib>
-
using namespace std;
-
struct set
-
{
-
long int mstoprocess;
-
int noofchannels;
-
int skipnoofbytes;
-
}settings={37000,8,0};
-
int main()
-
{
-
int i;
-
static struct set settings; //if this is removed it works fine!!!!
-
cout<<settings.mstoprocess<<endl;
-
getchar();
-
return 0;
-
}
-
-
1 3406 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.co m 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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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
{
|
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.
|
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 debug_print_backtrace in the
file to see how it is included, and here's the output:
#0 require_once() called at
#1 require_once(\eKirje.textGrid.class.php) called at
#0 require_once() called at
#1 require_once(\eKirje.kanava.class.php)...
|
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;
| |
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?
|
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 emails.
After sweating over the pear installation on myWindows Machine (IIS,
PHP 4.4.6, MySQL), I cannot even run the tutorial script.. I get :
Fatal error: Cannot redeclare class
mail_queue_container:mail_queue_container_db in...
|
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 ( "192.168.100.3", $login, $password );
if ( $c ) {
if ( ! mysql_select_db ( $database ) )
|
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 xxx.php from 3 different
functions.so can u give me the solution.
Thanks®ards
Sri
|
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,...
|
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...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |