Need help with C++

JC0724

Junior Member
Oct 11, 2008
3
0
0
First off I want to say I appreciate anybody who will be able to help me. I am a college graduate and I have taken some programming classes before. HTML, JavaScript, UNIX shell scripting, one C++ class and one assembly language class. I majored in electrical engineering with focus on logic design/digital systems. I don't program at my job now at all.

So anyway I decided I wanted to try programming again. So since my company pays for it I register for a class. The first of 3 classes which if I finish/pass all 3 I will be certified in C++.

I am not sure if this was a bad idea but as it stands I have been out of school for 3 years and all of the programming classes I took in my first 2 years(except assembly).

The first class assumes that u are already fluent in the basics of C++. I would drop the class and take a few refresher courses but it is too late for that so I am just going to do my best.

Anyway with that being said here are some of my questions.

I am trying to understand these following topics:

Pointers

What is the difference between?

P1 and *P1?

Why if I say

int *P1, *P2;
P1=new int;
*P1=42;
P2=p1;

Why does *P2 have the same value of 42 now?

Dynamic Arrays (I don't understand at all so I would appreciate any help or any examples on this for explanation)

Classes(Public and Private)(I kind of understand Classes and Structs I just don't get why we use them, wouldn't it be easier just to use regular functions and local variables) And with that being said that probably means I really don't understand them, lol. Any help with explanations on these would be greatly appreciated

I have a few more questions.

What are the rules for posting code on the forum?

Can I put up my own code and ask for help or hints on how to make it more accurate?

Can I post up code from my book/class and ask for understanding?

Thanks to anybody that is welling to help.



 

deveraux

Senior member
Mar 21, 2004
284
0
71
Pointers and dynamic/run time arrays will take some experience to get used to, there are precious little ways around it. They are important to your career as a programmer and are powerful tools. The link provided by net explains pointers well, but I will try my hand so you have 2 references to help you.

now, let us use a simple code such as

int p, *ptr;

Now, p is declared to be of type int and so the C++ abstraction will treat it as an integer unless type casted to another type (if you haven't reached this point in your studies, ignore it). *ptr OTOH is now a pointer which points to some point in memory. It is important to note that pointers do not really rely on their base type. That is to say, a pointer declared such as "char *ptr" or "void *ptr" or "long *ptr" or any other type is equivalent to int *ptr.

They just point to a location in memory. When you DEREFERENCE them however (as you do in your pasted code by using the asterisk before the variable, then they become their base type). Essentially, it is a variable containing a memory address. Any value it stores will not be read as an integer but as a location in memory that *should* contain the data you want.

The reason why I say *should*, is because when first declared, it will point to a random point in memory (potentially out of your programs legal access), hence why you should initialize or make it point to NULL.

Now, let us look at the code you used:

int *P1, *P2;
P1 = new int; // P1 now contains the memory location of a newly allocated space of type int
*P1 = 42; // dereferencing P1 and assign the value 42 to that location in memory
P2 = P1; // making P2 point to the same location as P1

I have added the comments to your code above. Basically, the reason *P2 has the same value as *P1 is because they are both pointing to the same location in memory (which contains the value 42).

Ok, now, dynamic arrays. I hope you have done normal arrays. Any normal array would be something like the following:

int p1[] = { 3, 4, 5, 6 };

Now, in actual fact, both p1 and str are actually pointers. They point to the first element in the array (arrays are sequential within memory).

p1[0] contains the value 3
p1[1] contains the value 4
and so on...

Now dynamic arrays are similar the only difference is that if I wanted a to declare the same array as above dynamically, it would done like the following:

int *p2;

p2 = new int[4];
p2[0] = 3;
p2[1] = 4;
p2[2] = 5;
p2[3] = 6;

Now, both p1 and p2 have the same array (but not necessarily the same memory pointer). This array is called dynamic just because its size can be determined during run time instead of a fixed size known during compiling.

As a side note about pointers:
If you execute the following code:
p1 = p1+1;

or

p1 ++;

p1[0] will now be 4 instead of 3.

As I said above, p1 points to the first element, so the code is telling p1 to move the size of 1 full int ahead (in the case of a 32-bit system, 4 bytes). Hence, p1 is now pointing to the next element in the array (because they are stored in memory sequentially).

Classes are used for object oriented programming (OOP). I suspect that your classes are more for the basic grounding in C++ which involves mostly procedural programming hence, classes will make little sense. There is some debate whether C++ is really an object-oriented language, but that's a separate discussion.

More on point, classes are useful when you create large programs and you want to reuse several parts of it. I probably did not explain that well, but in the end, its just another level of abstraction to make it easier to code large programs. Maybe someone else can explain it better? If I think of something, I'll add it in here.

As for the rules, I believe (and others please correct me if I am wrong), you can post code and ask for help, but only in the concepts, do not post homework. We are not here to do your homework for you but I'm sure most will be happy to either help or at least point you in the right direction to learn the important concepts.

HTH, good luck.

[Edit]: Doh! I didn't check myself, there is already a theard on pointers. You should check there for more info: http://forums.anandtech.com/me...=2233290&enterthread=y
 
Sep 29, 2004
18,656
68
91
tutorials online will teach just as well. I can't believe a company is paying for someone to learn basic C++ programming.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Originally posted by: IHateMyJob2004
tutorials online will teach just as well. I can't believe a company is paying for someone to learn basic C++ programming.

Cross training - as he stated, he is not a software person.
It would be like a Web page programmer going out to takea course in VSLI circuits design, being expected to know about MOSFETS