# Thread: Pointer question

1. ## Pointer question

Hi!

How do I use pointers if i need to let the user input an unknown number of numbers or characters?

I need to do this because i have to create a program that will be able to
perform exponentiation of square matrices. And there is no limit on the size of the matrix.

THANKS!

2. I would recommend making a buffer variable that will for sure have enough room to hold the maximum input. From there, you should know both the data and the length of the data and could create a pointer that points to the data. This pointer could point to a position in this buffer, or you could create space for this pointer via functions like malloc, realloc, calloc.

3. thanks!

can i use malloc if the maximum size of the matrix is not known since there really is no limit to the size of the matrix?

4. an example code would really be helpful. thanks!

5. That is the problem, we don't know what size the input will be. malloc() needs to know the size ahead of time. That is why I suggested using a buffer variable (kind of like a middle man) so you can get the information you need in order to create the variables you want.

6. oh ok. thanks! but i haven't used a buffer variable before. do you have a link where there's an explanation on how to use a buffer variable?

7. or a sample code?

thanks!

8. Originally Posted by myphilosofi
oh ok. thanks! but i haven't used a buffer variable before. do you have a link where there's an explanation on how to use a buffer variable?
There's kind of two questions going on. You normally use a buffer when you're inputting large amounts of text. But it sounds like you're not inputting large amounts of text. All you need to do is input into a variable (you could call it a buffer, but most people wouldn't) the size of the matrix.

9. -nevermind-

I was misunderstanding you. tabstop is onto what your looking for.

10. First, ask the user for the size of the matrix (x and y), and then use malloc to create a matrix of that size (if possible).

Your description of the input (letters and numbers) is so vague I'm just chuckling here. If you want decent help, you'll have to do better with your descriptions, than that.

11. unfortunately, i can't ask the user to input the size of the matrix.

here is the exact problem: it is attached

12. Then you're going to have to work at the input a fair bit. fgets is safe, so you can use that to read in a line and count how many numbers are in that line with some kind of loop (sscanf maybe, or strtod inside it). You will have to check that you got the whole line; if your input doesn't end in \n, you didn't get it all, so get some more and do it again.

13. thanks tabstop!

so this is what i'm planning to do:

1.let user input numbers (required: separated by one or more spaces)
loop as long as there is a space (since the exponent should just be an integer).

2. use strtok to split the strings into smaller strings

3. use strtod to convert the strings to a double.

my problem now is how and where to store the initial strings from fgets?
this is really my main problem.

14. Code:
`fgets(buff, 79, filehandle);`
Will put 79 chars into your buffer (a char array with room for 80 char's), from the file with filehandle.

From there you can use sscanf() or strtok or whatever, to get your numbers into their proper variables.

15. hmm. i don't think that's the best way.

based on the attached image of the problem, i should use pointers to "store" the matrix since the limit of the matrix size is unknown or infinite.

but i don't know how to do that since from what i've learned so far, pointers just store addresses. so how do i use pointers for it to store a "string"?

THANKS!

Popular pages Recent additions