Thread: need some help with strings/arrays/pointers

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    10

    need some help with strings/arrays/pointers

    I'm really unfamiliar with arrays and pointers, and one of my problems for homework involves both -_- I really have no idea where to go with it, so i'll just list the problem and see if you guys can get me going

    4A) write a declaration to store the string "this is a sample" into an array named samtest. Invlude the declaration in a program that displays the values in samtest using a for loop that uses a pointer access to each element in the array
    4B) modify the program written in exercise 4a to display only array elements 10 through 15 (these are the letters s, a, m, p, l, and e).

    as always, thanks for any help in advance

  2. #2
    Registered User
    Join Date
    Mar 2010
    Posts
    109
    Arrays are basically pointers. I think if you are having trouble on that level, you need to read more about the topic and then come back and ask more specific questions.

    Cprogramming.com Tutorial: Arrays

    Edit:

    Sounds like your teacher wants you to be using C-style strings, since the instructions mention storing a string in an array. This might help too:
    http://www.cprogramming.com/tutorial/lesson9.html
    Last edited by syzygy; 04-22-2010 at 02:14 PM.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    10
    i just dont really understand the crap, I've tried, right now i just need help cause this was due last week

    i should be more specific i guess, i kinda get string and arrays, pointers are the thing i have no clue on
    Last edited by rllove37; 04-22-2010 at 02:19 PM.

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    109
    A pointer is just a variable that stores the address of a location. So it 'points' to something else. You can set it to point to different parts of the array and, therefore, get the data that is stored there.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    109
    I don't really know what your teacher is asking for. The question does seem a little odd. I don't think that pointer use is really what he/she is looking for. Just ignore that part and use arrays. Use [] to access elements in the array.

  6. #6
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    Quote Originally Posted by syzygy View Post
    I don't really know what your teacher is asking for. The question does seem a little odd. I don't think that pointer use is really what he/she is looking for. Just ignore that part and use arrays. Use [] to access elements in the array.
    I think it might mean do something like
    Code:
    for (p=samtest; p<samtest+strlen(samtest); ++p) putchar(*p);
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    109
    Quote Originally Posted by NeonBlack View Post
    I think it might mean do something like
    Code:
    for (p=samtest; p<samtest+strlen(samtest); ++p) putchar(*p);
    Yeah, that's what I was thinking, too, but I wasn't sure with the way it's worded or the reasoning behind it. The combination of wording and lack of context (in regards to what the teacher is trying to teach) made the question a little ambiguous in my head.

    You are probably right, though.

  8. #8
    Registered User
    Join Date
    Mar 2010
    Posts
    10
    thanks. i had a friend explain pointers and it was 1000x easier than what my teacheer was talking about, also i figured it out

Popular pages Recent additions subscribe to a feed