Solved

can someone explain this to me?

Posted on 2007-11-19
9
255 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

746 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

9 Experts available now in Live!

Get 1:1 Help Now