Solved

can someone explain this to me?

Posted on 2007-11-19
9
282 Views
Last Modified: 2010-04-15
hi experts.
i have these codes for C programming....
now i'm having trouble understanding the codes...
could someone please explain why, how and what these codes r being used?
thnx

#include <stdio.h>

 #define MAX 4

 /* Define a structure, then declare and initialize */
 /* an array of 4 structures. */

 struct part {
     int number;
     char name[10];
 };
 /* Declare a pointer to type part, and a counter variable. */


struct part data[MAX] =
                {
                    {1, "Smith"},
                    {2, "Jones"},
                    {3, "Adams"},
                    {4, "Wilson"}
                };

 /* Declare a pointer to type part, and a counter variable. */



 struct part *p_part;
 int count;

 int main(void)
 {
     /* Initialize the pointer to the first array element. */

     p_part = data;

     /* Loop through the array, incrementing the pointer */
     /* with each iteration. */

     for (count = 0; count < MAX; count++)
     {
         printf("\nAt address %p: %d %s", p_part, p_part->number,
                 p_part->name);
         p_part++;
     }

 }
0
Comment
Question by:tsburt
9 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 20316973
What do you need explained?
Are the comments or the  C documentation for something confusing?
0
 
LVL 1

Author Comment

by:tsburt
ID: 20317010
yea i ahve a diff time understanding why these certain codes r used. i basically just started learnign C and if someone could explain line by line why this is need and what it does . i would be greatfull
0
 
LVL 12

Expert Comment

by:PCableGuy
ID: 20317054
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 53

Accepted Solution

by:
Infinity08 earned 125 total points
ID: 20318173
>> i basically just started learnign C

Then don't jump into the deep immediately. Get some C basics first (like the link PCableGuy listed).

This one looks pretty good :

       http://www.physics.drexel.edu/courses/CompPhys/General/C_basics/c_tutorial.html

After you've read and understood that (try everything out using your compiler while you're reading), you should be able to understand what the code you posted is doing. If you still have problems then, then we'll be happy to help you :)
0
 
LVL 1

Author Comment

by:tsburt
ID: 20325692
so basically what the define max 4 is doing is defining that there r 4 data : smith , jones, adams, wilson.
but what does the
struct part {
     int number;
     char name[10];

do is this statement?
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20326128
>> do is this statement?

No, that's a struct definition. It defines a new datatype that is a grouping of an int and a 10 character array.

Once it's defined, you can start using it, ie. make an instance called myPart :

        struct part myPart;

then set some values in myPart :

        myPart.number = 5;
        strcpy(myPart.name, "jack");

So, now myPart has number 5, and name "jack".

A struct is a convenient way of grouping data of different types together.
0
 
LVL 1

Author Comment

by:tsburt
ID: 20326187
ok i get it ..thnx infinity.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20326204
Do you understand the rest of the code ?
0
 
LVL 1

Author Comment

by:tsburt
ID: 20348338
yes...new struc p_part = to the data
refering the p_part to teh info from data
and than print
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.

838 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question