i'm just trying to experiment on these codes and I wanted to know the difference between the two.
On the first one, I tried assigning the variable[0].name[max] globally but an error occurs when running the program.
#include<stdio.h>
#define max 10
struct sample{
char name[max];
int money;
}variable[max]; strcpy(variable[0].name,"name");
main()
{
clrscr();
printf("%s",variable[0].name);
getch();
}
Argument List syntax error
but if do it this way, i assign the value inside the main:
#include<stdio.h>
#define max 10
struct sample{
char name[30];
int money;
}variable[10];
main()
{
clrscr();
strcpy(variable[0].name,"name");
printf("%s",variable[0].name);
getch();
}
it works out..
could someone kindly explain to me the reason why this happens in C.
thnx:)
8 1692
i'm just trying to experiment on these codes and I wanted to know the difference between the two.
On the first one, I tried assigning the variable[0].name[max] globally but an error occurs when running the program.
#include<stdio.h>
#define max 10
struct sample{
char name[max];
int money;
}variable[max]; strcpy(variable[0].name,"name");
main()
{
clrscr();
printf("%s",variable[0].name);
getch();
}
Argument List syntax error
but if do it this way, i assign the value inside the main:
#include<stdio.h>
#define max 10
struct sample{
char name[30];
int money;
}variable[10];
main()
{
clrscr();
strcpy(variable[0].name,"name");
printf("%s",variable[0].name);
getch();
}
it works out..
could someone kindly explain to me the reason why this happens in C.
thnx:)
you can declare variables but you can't run code outside functions.
i'm just trying to experiment on these codes and I wanted to know the difference between the two.
On the first one, I tried assigning the variable[0].name[max] globally but an error occurs when running the program.
#include<stdio.h>
#define max 10
struct sample{
char name[max];
int money;
}variable[max]; strcpy(variable[0].name,"name");
main()
{
clrscr();
printf("%s",variable[0].name);
getch();
}
Argument List syntax error
but if do it this way, i assign the value inside the main:
#include<stdio.h>
#define max 10
struct sample{
char name[30];
int money;
}variable[10];
main()
{
clrscr();
strcpy(variable[0].name,"name");
printf("%s",variable[0].name);
getch();
}
it works out..
could someone kindly explain to me the reason why this happens in C.
thnx:)
hi there,
your statement "strcpy(variable[0].name,"name");" was declared outside to the beginning of the definition of the main function. The main function is the point by where all C++ programs start their execution, independently of its location within the source code. It does not matter whether you declare other functions with other names defined before of after it - the instructions contained within this function's definition will always be the first ones to be executed in any C++ program. For that same reason, it is essential that your statement should be declare inside the main.
regards,
hi there,
your statement "strcpy(variable[0].name,"name");" was declared outside to the beginning of the definition of the main function. The main function is the point by where all C++ programs start their execution, independently of its location within the source code. It does not matter whether you declare other functions with other names defined before of after it - the instructions contained within this function's definition will always be the first ones to be executed in any C++ program. For that same reason, it is essential that your statement should be declare inside the main.
regards,
farmer,
i have learned that in C, there is what we call global declaration in which you can declare and assign values outside or before the main function.
let us take this example:
#include<stdio.h>
char name='a';
main()
{
clrscr();
printf("%c",name);
getch();
}
it displays the character 'a' when running the program.
in this example, i declared the character name and assigned value to it outside the main function.
knowing that this works, why not with structures..
hi there,
your statement "strcpy(variable[0].name,"name");" was declared outside to the beginning of the definition of the main function. The main function is the point by where all C++ programs start their execution, independently of its location within the source code. It does not matter whether you declare other functions with other names defined before of after it - the instructions contained within this function's definition will always be the first ones to be executed in any C++ program. For that same reason, it is essential that your statement should be declare inside the main.
regards,
farmer,
i just figured it out this time = )
here is the modified one.
#include<stdio.h>
struct nef{
char name[10];
int balance;
}dota;
struct nef dota={"neftali",1000};
main()
{
clrscr();
printf("%s %d",dota.name, dota.balance);
getch();
}
=) the only problem is the syntax of assigning values to the different fields on the structure.
anyway, thank you guys: )
farmer,
i just figured it out this time = )
here is the modified one.
#include<stdio.h>
struct nef{
char name[10];
int balance;
}dota;
struct nef dota={"neftali",1000};
main()
{
clrscr();
printf("%s %d",dota.name, dota.balance);
getch();
}
=) the only problem is the syntax of assigning values to the different fields on the structure.
anyway, thank you guys: )
This is important to understand. You are not assigning values to variables outside of functions. You are declaring and defining variables. Big difference.
This is important to understand. You are not assigning values to variables outside of functions. You are declaring and defining variables. Big difference.
so does that mean it is not advisable to assign values to variables outside the function?
I dont get it:( However, the code i posted recently works out..
farmer,
i just figured it out this time = )
here is the modified one.
#include<stdio.h>
struct nef{
char name[10];
int balance;
}dota;
struct nef dota={"neftali",1000};
main()
{
clrscr();
printf("%s %d",dota.name, dota.balance);
getch();
}
=) the only problem is the syntax of assigning values to the different fields on the structure.
anyway, thank you guys: )
the scope of variables declared within a function or any other inner block is only their own function or their own block and cannot be used outside of them.
#include <iostream.h>
int integer -----------
char aCharacter; // this ------------- first four are global variables
char string [20]; -------------
unsigned int YourNumber; -----------
main ()
Yes, you have a point global variables; These are visible from any point of the code, inside and outside all functions. In order to declare global variables you simply have to declare the variable outside any function or block; that means, directly in the body of the program. but on your program before you did'nt declare that statement as global variable so you can't get the same output as you desire..
anyway I'm glad you'd done it well
regards,
so does that mean it is not advisable to assign values to variables outside the function?
I dont get it:( However, the code i posted recently works out..
In the code that you posted: - struct nef{
-
char name[10];
-
int balance;
-
}dota;
-
-
struct nef dota={"neftali",1000};
there is no assignment such as
You might find it easier to look at your code slightly differently:
declaration of struct type, nef: - struct nef{
-
char name[10];
-
int balance;
-
};
declaration of single instance of type nef:
declaration and definition of single instance of type nef: - nef dota = {"neftali",1000};
This is all good. It initialises the struct and gives it global scope. assingment of a value to member of single instance of type nef: - dota.balance = 253; //cannot perform this action outside of function
This will generate a compiler error. This is an assignment operation that cannot be executed outside of a function.
Hope this helps
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Sims |
last post by:
Hi,
I have some small questions that have never been any problems, (for my
compiler?), but always make me curious.
So here goes...
what does the standard sday about the 'if' statement?
for...
|
by: kazack |
last post by:
Hi all it's me again with another question as I got further in my book. The
chapter I am in covers structres, abstract data and classes. I only read
through to the end of the coverage on...
|
by: Dave H |
last post by:
Hello,
I have a query regarding definition lists. Is it good practice
semantically to use the dt and dd elements to mark up questions and
answers in a frequently asked questions list, or FAQ?
...
|
by: Steve Jorgensen |
last post by:
In a recent thread, RKC (correctly, I believe), took issue with my use of
multiple parameters in a Property Let procedure to pass dimensional
arguments on the basis that, although it works, it's...
|
by: steve |
last post by:
I'm trying to create a structure of three pointers to doubles. For
which I have:
typedef struct {
double *lst_t, *lst_vc, *lst_ic;
} last_values;
I then need to allocate space for...
|
by: Alfonso Morra |
last post by:
Hi,
I am having some probs with copying memory blocks around (part of a
messaging library) and I would like some confirmation to make sure that
I'm going about things the right way.
I have...
|
by: Mr Newbie |
last post by:
I am often in the situation where I want to act on the result of a function,
but a simple boolean is not enough. For example, I may have a function
called
isAuthorised ( User, Action ) as ?????...
|
by: Steve Barnett |
last post by:
I have a database with two tables "Structure" and "Data". The Data table
contains the data for my application, so no problem there. The Structure
table effectively puts some organisation to the...
|
by: karan.shashi |
last post by:
Hey all,
I was asked this question in an interview recently:
Suppose you have the method signature
bool MyPairSum(int array, int sum)
the array has all unique values (no repeats), your...
|
by: Herbert Kleebauer |
last post by:
As part of a simple X demo for Linux I send the byte string
in "send8" to the X server. This byte string displays a circle
at the x/y position send8.s8x/send8.s8y. In "display()" a 8x8
grid of...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
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...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
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...
|
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...
|
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...
| |