Thread: help with pointers and strings

  1. #1
    Unregistered
    Guest

    help with pointers and strings

    I know there are a lot of questions here, but if anyone can post some responses to any/all, i would be extremely appreciative. I don't understand these things at all. I'm very new to programming.

    Ok...
    Let's start off with declaring strings and pointers.

    void main(void) {
    char string1[70];
    char *pointer1, *pointer2;
    char string2[70];
    pointer1 = &string1;
    pointer2 = &string2;

    ok, tell me if i'm reading my code correctly.
    pointer1 = the address of string1????
    *pointer1 = 1st value of string1????

    2nd question.
    let's say I have string4[] = "hello";
    char *pointer4;
    pointer4 = &string4;

    I want to use the 2nd value of the string...so would I use the notation:

    *(pointer4 + 1); would this give me the 'e' value?

    3rd question.
    I've come across a gets() function.
    It inputs a string from a user. How exactly does this work?
    Let's say I wanted to input my string1 value using gets().

    Would this work?

    char gets(*pointer1);


    4th question:
    I've come across another function... isAlpha();
    I can't figure this one out either.
    Let's say I wanted to check a string of int, char, and float values... and put all of the char values into another string.

    Would this work?

    for(int i=0; i<70;) {
    if(isAlpha(*(pointer1 + i))) {
    *(pointer2 + i) = *(pointer1 + i);
    i++;
    }
    else
    i++;
    }

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    char string1[70];
    char *pointer1, *pointer2;
    char string2[70];
    pointer1 = &string1;
    pointer2 = &string2;

    ok, tell me if i'm reading my code correctly.
    pointer1 = the address of string1????
    *pointer1 = 1st value of string1????
    Yes, although the variable names of arrays are themselves treated as addresses so you don't need to use the "address of" operator in this case. You could have just said:

    Code:
    pointer1 = string1;
    pointer2 = string2;
    2nd question.
    let's say I have string4[] = "hello";
    char *pointer4;
    pointer4 = &string4;

    I want to use the 2nd value of the string...so would I use the notation:

    *(pointer4 + 1); would this give me the 'e' value?
    Yes, but once again just say pointer4 = string4;. You could also treat the pointer variable as if it were an array itself as in pointer4[1] which would also equal 'e'.

    3rd question.
    I've come across a gets() function.
    It inputs a string from a user. How exactly does this work?
    Let's say I wanted to input my string1 value using gets().

    Would this work?

    char gets(*pointer1);
    The gets function takes a character pointer as an argument, therefore you would not dereference the variable. The gets function reads a line from the standard input stream stdin and stores it in a buffer (the argument you supply to the function). The line consists of all characters up to and including the first newline character ('\n'). gets then replaces the newline character with a null character ('\0') before returning the line. To use this, do something like this instead:

    Code:
    gets(pointer1);
    4th question:
    I've come across another function... isAlpha();
    I can't figure this one out either.
    Let's say I wanted to check a string of int, char, and float values... and put all of the char values into another string.

    Would this work?

    for(int i=0;i<70; )
    {
    if(isAlpha(*(pointer1 + i)))
    {
    *(pointer2 + i) = *(pointer1 + i);
    i++;
    }
    else i++;
    }
    The function is not isAlpha, it is isalpha and it returns a non-zero value if its argument is within the ranges A – Z or a – z. You code is close but not quite right, your second string could have gaps in it because of your use of the i variable. You would want to use something like this instead:

    Code:
    int i, j;
    for( i=0, j=0; i < 70; i++ )
    { 
        if( isalpha( *(pointer1 + i) ) )
        { 
            *(pointer2 + j) = *(pointer1 + i); 
            j++;
        } 
    }
    With the added variable j only getting incremented when we have found a character to add to the second string, we will ensure that there are no gaps in the string pointed to by the variable pointer2 which could otherwise have garbage in it.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with calloc and pointers to strings.
    By sweetarg in forum C Programming
    Replies: 1
    Last Post: 10-24-2005, 02:28 PM
  2. Arrays, pointers and strings
    By Apropos in forum C++ Programming
    Replies: 12
    Last Post: 03-21-2005, 11:25 PM
  3. Concatenating strings (dynamic array using pointers)
    By Tankndozer in forum C Programming
    Replies: 8
    Last Post: 07-01-2004, 07:27 AM
  4. hangman need help with strings and pointers
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 03-27-2002, 09:13 AM
  5. need more help with pointers and strings
    By bgbfflochp in forum C++ Programming
    Replies: 11
    Last Post: 03-19-2002, 08:31 AM