Pointers (archived tutorial)

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2005
Posts: 15,412
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1469
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Pointers

 
0
  #11
Oct 30th, 2006
>>*pch = ch[0];
Example 1-1

Above line is incorrect because pointer pch is uninitialized.

>>ptr = &n[0];
Example 1-3.

This can also be simplified like below: The & operator is not necessary.
ptr = n;

>>ca[3] = '\0';
Figure 2-1
The above is wrong because array ca only has 3 elements, not 4.

>>ptr = &text[0]; // not recommen
Example 2-2

Why is this not recommended? You did not explain that statement.

Example 2-3: I disagree -- it seems to add a lot more complexity to such a simple and elementry operation. The example in Example 2-2 works just find and is the one most frequently used.

>>ptr = (char *)malloc(6);]
Code 2-1
malloc() should not be typecase in C, but is required in C++

Example 3-1 is a very good example of why not to use strcpy() -- because it can easily cause buffer overrun and scribble all over the program's memory.

>> ptr = (char **)malloc(rows * (sizeof *ptr)); // allocate room for rows
Code 3-1
sizeof(*ptr) will result most likely always return 1 becuase sizeof(char) is always 1. This is clearly not what you are attempting to do. The program needs to allocate room for rows number of pointers, like this:
  1. ptr = malloc(rows * sizeof (char*)) // allocate room for rows

>> ptr[i] = (char *)malloc(strlen(word[i]) + 1 * (sizeof **ptr));

Same problem as above. Should be this:
  1. ptr[i] = malloc(strlen(word[i]) + 1 * sizeof(char **)));

Figure 4-1 needs some rework to fix up the sizeof() errors, as identified above. sizeof(char*) is not the same as sizeof(char) on most operating systems. And the sizeof(*Book) is the same thing as sizeof(char) -- guarenteed by C standards to always be 1.


Overall, give you B+ for good tutorial. Correct the mistakes and it can be a great tutorial.
Last edited by Ancient Dragon; Oct 30th, 2006 at 2:17 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 1
Reputation: PeterPimmel is an unknown quantity at this point 
Solved Threads: 0
PeterPimmel PeterPimmel is offline Offline
Newbie Poster

Re: Pointers

 
0
  #12
Apr 26th, 2008
Originally Posted by Ancient Dragon View Post
  1. ptr[i] = malloc(strlen(word[i]) + 1 * sizeof(char **)));

Overall, give you B+ for good tutorial. Correct the mistakes and it can be a great tutorial.
nah, this was wrong anyway.

SO BE CAREFUL FOLKS! it has to be:
  1. ptr[i] = malloc((strlen(word[i])+1) * sizeof(char **)));
instead of
  1. ptr[i] = malloc(strlen(word[i]) + 1 * sizeof(char **)));
you know * binds stronger than + ... good old math :-P

damn someone should change this in the tutorial... I took me some nerves to find out why my app was crashing :-P
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC