Pointer Arrays
Hello,
(Note: This problem comes straight from my course textbook. But, it is not for a grade, nor is it homework. I do the exercises to learn the material, but the professor doesn't assign them - he doesn't even mention them) This is from the chapter on C-Strings: 6. Write a function that will accept ten lines of user-input and store the entered lines as ten individual C-strings. Us a pointer array in your function. Here is what I have: Code:
#include <iostream> Code:
#include <iostream> This is the point where I am stuck. Any help would be appreciated. |
Re: Pointer Arrays
Quote:
|
Hello,
Quote:
|
Forgive me for saying so, but you are wrong in what you just said,
Quote:
PAix |
Hello,
The problem tells me to use a pointer array. And the book says: Quote:
I am sure that you guys know what exactly is wrong. And you're right, I don't want just an answer, I want mostly an understanding. But the clue given to me isn't jump-starting my brain. I just don't see anything wrong with my pointer array declaration, sorry. :scratch: |
Quote:
Quote:
What is the value of temp? Did you even know that temp could have a value? This is C/C++, not BASIC, so you have to do some heavy lifting. Look up the functions malloc, calloc, and free in your textbook. They will probably have some examples of what you are looking for. You will also want to look at strcpy and memcpy. There is an important difference between the two. |
This exercise in pure C is far from trivial. You would help yourself by learning exactly what pointers are and how they work. For example if you dont understand what this does.
Code:
char array[30]; You said above that you 'assume', with a programming language like C/C++ you need to know. The best advice i can give you is, "learn the fundamentals" NOW about your question. Code:
When you declare *textl[10] you are declaring an array of pointers to type char. 81 consecutive characters to which &temp[0] is the start of that array. Code:
When you say textl[0] = temp , you are saying point the pointer at textl[0] to the start of the char array temp, infact, textl[0] = temp is just like saying If in your example you had allocated the memory in all of those pointers then you would have no problem, for example if you had put Code:
for (int a = 0;a < 10 ;a++) just before you try to read all those strings in then it would work. Do you understand? The above code will initialize all the pointers to hold 81 chars. So when you then go getline(textl[i],81), the pointer your getline() function is writing into actually has somewhere to write to. You not understanding why your getting a segmentation fault is due, to not understanding how the language works. For example being able to tell what a statement like: char *bleh = "this is a string" actually does is very important. Not knowing what is really going on here allows you to assume things like getline(textl[i], 81) shouldnt fail, i mean if i can assign a sentence into a pointer when i declare it, i should be able to read a string into a an un initialized pointer right? wrong! For the above example when you write char *bleh = "some stuff", what happens is c declares some memory say enough to hold the string, which in this case is 10 chars, which you could do by calling malloc, memory allocation, like malloc(11), next it will fill 10 of those chars up with the string "some stuff", it will then use that last char to null terminate the string, that is the char '\0'. It then will send back a pointer to the first character in that array and assign the symbol bleh to it. Code:
So you have a pointer to some memory that would be mapped out like this -> s o m e s t u f f \0, the pointer is pointing to Now malloc as its name suggests allocates memory. so malloc(23) will allocate 23 chars of memory and send back a void pointer. So char *p = (char *)malloc(23) will allocate 23 chars like ( char men[23] ) and return back a void pointer which you then cast to a char * pointer and assign it to the symbol p. Now you can reference all those chars like p[0] p[1] etc and assign chars into them like p[0] = 's' p[1] = 'o'.You must learn all of this and more, C and C++ are fantastic languages, but they deserve your undivided attention. Do you see why the guys above said what they did, because they cannot spend all this time to explain to you how the C language works, which is the only way to properly answer your question. Instead they are pointing you in the right direction to learn all this stuff yourself. |
Hello,
I got the program working. Here is the final code: Code:
#include <iostream> Thanks for all your help. |
Hello,
Quote:
Quote:
I know it must be trying talking to a learning programmer like me, but I appreciate the help. |
Hello,
Quote:
Again, thanks for all the help. |
Ah its a pleasure to help a beginning programmer, IF, they listen to what you are saying. I recommend looking at all the stuff i wrote above, if you can re-read it you will see things like
Quote:
Here is an exercise for you, write me a post back, explaining WHY the 'new' works AND why this did NOT work Code:
for (i=0; i<10; i++){ |
Quote:
|
what you really want to be able to do is something like this
;) functions.h Code:
#ifndef FUNCTIONS_H Code:
#include "functions.h" Code:
#include <stdio.h> |
OK this is my last post, here is the updated C++ version
function.h Code:
#ifndef FUNCTION_H function.cpp Code:
#include "function.h" main.cpp Code:
#include "function.h" DYNAMIC than the actual C++ programming languages implementation of getline is. To compile use Code:
g++ -g -c function.cpp |
Re: C'est horrible
Quote:
Where is the check for NULL in the return value from malloc? malloc can fail... |
All times are GMT -5. The time now is 06:02 AM. |