Thread: pointer and strings

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    6

    pointer and strings

    how do you use pointers to point at individual elements of a string. Because i want to have a pointer (*p) pointing at the first element of the string strng[0], and a second pointer (*q) pointing at the last element of the string.

    say if the string was "go dog"

    (which is a palindromes..which spells the same string foward and backward,,,,thats why i need help on this part of the project)

    can anybody help?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    char buffer[50] = "go dog";
    char *front, *back;

    front = buffer;
    back = buffer[strlen(buffer)-1]; // or "buffer + (strlen(buffer)-1)"

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    6
    for the pointer (back) it said there was an errror and that it cannot convert from char to char*.

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    TRY

    front = buffer;
    *back = buffer[strlen(buffer)-1]; // or "buffer + (strlen(buffer)-1)"
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    6
    ok that second one works... but when i try to print the first element it prints the entire string and i try to print the second element all it prints is 'e' all the time. I need to only print the first element and the last element because i have to be able to compare the elements to see if they are the same.. I am working on this homework assignment which tells us test if the string is a palindrome...(ex. "go dog" because its the same sentence read backwards)...so my plan is to check if the first element is same as last and then i will.....(first++, and last--) until the string has reached the middle and the program has tested it.

  6. #6
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    You could just say "The dog ate your homework"

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    6

    to brian

    a Brian why dont u shut your ass up .... your not helping

  8. #8
    Registered User C of Green's Avatar
    Join Date
    Jan 2002
    Location
    Calgary/Canada
    Posts
    59
    lostpoet ... relax man ... that was funny

  9. #9
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    Well, gee, I'm sorry, but I am fed up of worthless little craps posting crappy annoying paladrome questions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. comparing 2 strings with pointer
    By meriororen in forum C Programming
    Replies: 9
    Last Post: 05-22-2009, 07:37 PM
  2. fscanf's auto-allocation for strings
    By @nthony in forum C Programming
    Replies: 12
    Last Post: 07-03-2006, 06:47 AM
  3. Pointer strings
    By AProg in forum C Programming
    Replies: 14
    Last Post: 05-22-2003, 07:07 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. pointer to pointers to arrays of strings??
    By Binkstone in forum C Programming
    Replies: 8
    Last Post: 09-14-2001, 02:56 AM