The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
What does the line stand for
Discuss What does the line stand for in the C Programming forum on Dev Shed. What does the line stand for C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 23rd, 2013, 11:27 AM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 14
Time spent in forums: 1 h 43 m 25 sec
Reputation Power: 0
|
|
|
What does the line stand for
Hi everyone. I have a programe and I'm trying to understand what's going on... in one of the lines.
the code is the following:
Code:
int main()
{
srand((unsigned) time(0));
double PI = 3.14159265358979323846;
point points[N], randompoint;
int i = 0;
int j;
//generate N different points
while (i < N) {
randompoint.x = (double) (rand() % XLIM);
randompoint.y = (double) (rand() % YLIM);
int exist = 0;
for (j = 0; j < i; ++j)
if (points[j].x == randompoint.x && points[j].y == randompoint.y) {
exist = 1;
break;
}
if (!exist) {
points[i].x = randompoint.x;
points[i].y = randompoint.y;
++i;
}
}
My question, is about the line: point points[N], randompoint;
What does it do? Or what kind of "thing" is it?
Maybe I should include the rest of the int main, let me know if this is not enough information.
Thanks in advance 
|

January 23rd, 2013, 11:47 AM
|
|
Contributing User
|
|
Join Date: Feb 2010
Posts: 66
Time spent in forums: 14 h 4 m
Reputation Power: 0
|
|
|
i think program is incomplete or u may have missed something..
point is a user defined datatype... it will be mostly a structure and its variables are x and y......
|

January 23rd, 2013, 11:52 AM
|
|
Contributing User
|
|
Join Date: Feb 2010
Posts: 66
Time spent in forums: 14 h 4 m
Reputation Power: 0
|
|
|
what is xlim ylim... even some header file may be missing and N must have a value...
structure may look like this
struct p{
int x,y;
}point;
|

January 23rd, 2013, 11:59 AM
|
 |
Contributing User
|
|
Join Date: Aug 2003
Location: UK
|
|
Code:
point points[N], randompoint;
Declares two variables, an array of points and a single point. It is the same as:
Code:
point points[N] ;
point randompoint ;
but is written by terminally lazy coders.
|

January 23rd, 2013, 12:58 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
N, XLIM, and YLIM are undoubtedly macros created by #define statements; eg:
Code:
#define N 100
#define XLIM 42
#define YLIM 42
When you compile the program, first the pre-processor will scan through the source code and replace every occurance of a macro name with the string that it's defined as being; eg, assuming the example #define's above, this
Code:
point points[N], randompoint;
int i = 0;
int j;
//generate N different points
while (i < N) {
randompoint.x = (double) (rand() % XLIM);
randompoint.y = (double) (rand() % YLIM);
will become
Code:
point points[100], randompoint;
int i = 0;
int j;
//generate N different points
while (i < 100) {
randompoint.x = (double) (rand() % 42);
randompoint.y = (double) (rand() % 42);
Like the missing declaration of the struct point, those missing macro definitions are probably above main in the upper portion of the program that was not included.
BTW, it is common coding convention to write all macro names in all-caps, so that when reading the source code you can immediately recognize the macros. And since by this convention no other identifiers (variable, typedef, or function names) are written in all caps, that allows you to "reuse" those names. Therefore, while N is a macro, you can still use n as a variable name.
|

January 23rd, 2013, 01:23 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 14
Time spent in forums: 1 h 43 m 25 sec
Reputation Power: 0
|
|
thank you for the clear and simple answer! 
|

January 23rd, 2013, 01:25 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 14
Time spent in forums: 1 h 43 m 25 sec
Reputation Power: 0
|
|
Thank you for your response.
Yes, indeed, those macros are above ind the program. 
|

January 23rd, 2013, 01:53 PM
|
|
Contributing User
|
|
Join Date: Feb 2010
Posts: 66
Time spent in forums: 14 h 4 m
Reputation Power: 0
|
|
|
if i am not wrong point is a structure right?
sorry i got it
Last edited by swapy : January 23rd, 2013 at 02:02 PM.
|

January 23rd, 2013, 02:11 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 14
Time spent in forums: 1 h 43 m 25 sec
Reputation Power: 0
|
|
|
Yes, it is. It's my mistake. Sorry
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|