| | |
Pointers (archived tutorial)
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
>>*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:
>> ptr[i] = (char *)malloc(strlen(word[i]) + 1 * (sizeof **ptr));
Same problem as above. Should be this:
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.
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:
C++ Syntax (Toggle Plain Text)
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:
C++ Syntax (Toggle Plain Text)
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.
•
•
Join Date: Apr 2008
Posts: 1
Reputation:
Solved Threads: 0
•
•
•
•
C++ Syntax (Toggle Plain Text)
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.
SO BE CAREFUL FOLKS! it has to be:
C++ Syntax (Toggle Plain Text)
ptr[i] = malloc((strlen(word[i])+1) * sizeof(char **)));
C++ Syntax (Toggle Plain Text)
ptr[i] = malloc(strlen(word[i]) + 1 * sizeof(char **)));
damn someone should change this in the tutorial... I took me some nerves to find out why my app was crashing :-P
![]() |
Similar Threads
- pointers to function problem (C++)
- <help> pointers? (C)
- Hi i'm new here (C++)
- C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays (C++)
- Pointers (C++)
- Floating point numbers (C++)
- Pointers (Part II) (C)
- ambigious part 4 (C)
Other Threads in the C++ Forum
- Previous Thread: Reading from file
- Next Thread: Quick Sort
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






