473,326 Members | 2,090 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,326 software developers and data experts.

Structure Variables within C language

2
I am begginr programmer wiyh C. I want to do a work with structure variables, but I don't know how. Could anyone help me?
I have to define a structure variable like bellow:

struct name{
int x;
int y;
}NAME;

I creat some NAME[i] within a loop and set "x" and "y" for all of them (for example 100 NAME[i]). I must also add that the "y" is a unrepeatable number and in all NAME[i] variales (i.e. there are 100 different "y"). After that I get a number (k) equal to one "y" of the NAME variables within another part of my code. I want with this number (k) get the related x (that its "y" is equivalent to this number).
My problem is that I don't know how can I do that?

Thanks in advance,

Cheers
Nov 6 '07 #1
5 6615
Well to start with, all caps (like NAME) is usually a precompiler constant in C, so you should use something like Name or name_s to represent that struct.

To your problem, you'll need a loop like for() or while(). You'll want to declare an array of your structure large enough to hold them all. I'm not sure what you mean about k, but if you post some code or pseudocode that gives a better idea then maybe others can help more.
Nov 6 '07 #2
Mehr
2
Hi,
Thank you for your reply.
My problem is that I don't want to use any loop for this issue, because the code will become very time consuming. I like to get a method through C that it can give me directly an apropriate NAME that its "y" is equal to my arbitrary number (for example k).
Nov 7 '07 #3
Well if your data is sorted you could look at bsearch().
Nov 7 '07 #4
primeSo
35
I am begginr programmer wiyh C. I want to do a work with structure variables, but I don't know how. Could anyone help me?
I have to define a structure variable like bellow:

struct name{
int x;
int y;
}NAME;

I creat some NAME[i] within a loop and set "x" and "y" for all of them (for example 100 NAME[i]). I must also add that the "y" is a unrepeatable number and in all NAME[i] variales (i.e. there are 100 different "y"). After that I get a number (k) equal to one "y" of the NAME variables within another part of my code. I want with this number (k) get the related x (that its "y" is equivalent to this number).
My problem is that I don't know how can I do that?

Thanks in advance,

Cheers
Hi. I was confused by your question at the first place.
You mean you wanted to create 100 instances of your structure called "NAME" is it ? Hope i am not mistaken. Well , the way to create a collection of variable with the type "NAME" should be as below:
Expand|Select|Wrap|Line Numbers
  1. NAME nameArray[100];
But the proper way to create a user define data type in C should be as follow:
Expand|Select|Wrap|Line Numbers
  1. Name nameArray[100];
. The above code create an array which can store 100 instances or variable of the user define type "NAME". Then your initialize the data member in each member of nameArray[] by using a loop. Note that each member of nameArray[] is indeed a structure that contain 1 integer type call "x" and 1 integer type call "y".

You have mentioned that you want to assign unique number to integer variable y in each variable of NAME type. U didn't mention the expected value of "x". Thus, my suggested method for your question is:
Create N number of variable NAME type and store them in an array as mentioned.
Then, initialize the variable using a for loop.
Expand|Select|Wrap|Line Numbers
  1. for( i = 0 ; i < 100; i ++) {
  2.    NAME [i].y = i;
  3.    NAME[i].x = i * i; /* i time i*/
  4.  }
By using the for loop, u assign a unique number to y and i x i to x. Note the dot operator (.) is used to access each member in every variable of type NAME. After the initilization , you could found your corresponding x by searching the corresponding y using a driver program.
You may need to do some reading on the structure.
I don't understand what do u mean by "not using loop". Then u have to use a sequential way to initialize the value of x and y.
Nov 7 '07 #5
primeSo
35
Hi,
Thank you for your reply.
My problem is that I don't want to use any loop for this issue, because the code will become very time consuming. I like to get a method through C that it can give me directly an apropriate NAME that its "y" is equal to my arbitrary number (for example k).
Don't understand your problem.
using loop is time consuming ? u mean the system wise ? not efficient ? C is a structure programming, there are only 4 ways to control the sequence to execute the program. 1 ) Sequence 2) Iteration 3)Select statement 4)Function call.

Try to do some reading , it would help a lot.
Nov 7 '07 #6

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

Similar topics

11
by: Mannequin* | last post by:
Hi all, I'm working on a quick program to bring the Bible into memory from a text file. Anyway, I have three questions to ask. First, is my implementation of malloc () correct in the program to...
7
by: Excluded_Middle | last post by:
Suppose I have a struct typdef struct foo { int age; char *name; }foo; now I made a list of foo using
3
by: Muhammad Farooq-i-Azam | last post by:
Hi, I am trying to define an arp structure but having problem doing so. I think I have define the correct arp structure but I find myself in a strange problem. The size of structure that I have...
5
by: masood.iqbal | last post by:
My simplistic mind tells me that having local variables within looping constructs is a bad idea. The reason is that these variables are created during the beginning of an iteration and deleted at...
2
by: Cybertof | last post by:
Hello, I would like to understand the difference between declaring a structure/variable in a class body, and doing the same thing in a procedure body. Example : In the following code, what...
2
by: Steve | last post by:
I have defined a structure of; Private Structure Menu Private MenuID as string Private Label as string End Structure ....and I would like to create and initialize an array of type "Menu". I...
7
by: Jimakos Bilakis | last post by:
Hi guys! I'm using the C++ Builder 6 Enterprise Edition where I create some tables in Paradox and with the help of a structure i pass my data from the form (Edit boxes) to the Paradox table with...
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
3
by: John Dann | last post by:
Trying to learn Python here, but getting tangled up with variable scope across functions, modules etc and associated problems. Can anyone advise please? Learning project is a GUI-based...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
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 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.