Solved

can someone explain this to me?

Posted on 2007-11-19
9
265 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
 
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
Promote certifications in your email signature

Has your company recently won an award or achieved a certification? They'll no doubt want to show it off. Email signature images used to promote certifications & awards can instantly establish credibility with a recipient and provide you with numerous benefits.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

910 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now