Pointer question

This is a discussion on Pointer question within the C Programming forums, part of the General Programming Boards category; Hi! How do I use pointers if i need to let the user input an unknown number of numbers or ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    22

    Question 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. #2
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    398
    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. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    22
    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. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    22
    an example code would really be helpful. thanks!

  5. #5
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    398
    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. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    22

    Smile

    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. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    22
    or a sample code?

    thanks!

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by myphilosofi View Post
    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. #9
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    398
    -nevermind-

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

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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. #11
    Registered User
    Join Date
    Jan 2009
    Posts
    22

    Smile

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

    here is the exact problem: it is attached
    Attached Images Attached Images  

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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. #13
    Registered User
    Join Date
    Jan 2009
    Posts
    22

    Smile

    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.

    please help..thanks!

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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. #15
    Registered User
    Join Date
    Jan 2009
    Posts
    22

    Smile

    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!

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Easy pointer question
    By Edo in forum C++ Programming
    Replies: 3
    Last Post: 01-19-2009, 09:54 AM
  3. char pointer to pointer question
    By Salt Shaker in forum C Programming
    Replies: 3
    Last Post: 01-10-2009, 10:59 AM
  4. Pointer question
    By rakan in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2006, 01:23 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21