pointer arithmetic with characters

This is a discussion on pointer arithmetic with characters within the C++ Programming forums, part of the General Programming Boards category; Hello, I am attempting to use pointer arithmetic to extract a certain portion of a string but have met nothing ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    59

    pointer arithmetic with characters

    Hello,

    I am attempting to use pointer arithmetic to extract a certain portion of a string but have met nothing but errors.

    I want to extract the characters from the original string (origStr) from the third character to the '.' character or the '_' character (whatever comes first).

    I would like to output it to the monitor.

    My code is below. Could someone tell me what is wrong?

    Code:
    #include <iostream.h>
    
    //begin main
    int main()
    {
    	char origStr[] = "alHello_0.shp";
    	char newStr[25];
    
    	//strip the name down to the component
    	*newStr = *(origStr + 2);	//set the pointer in a to the third character of b
    	
    	//eliminate the rest of the characters from either the _ or the . char
    	for( ; (*origStr != '_') && (*origStr != '.'); origStr++, newStr++) 
    	{ *newStr = *origStr; }
    
    	++newStr;
    
    	//place a null at the present character
    	*newStr = '\0';
    
    	cout << newStr << endl;
    
    	return 0;
    
    }	//end main
    Here are my compliation errors:


    c:\temp\c++\pec\pec\test.cpp(13): error C2105: '++' needs l-value
    c:\temp\c++\pec\pec\test.cpp(13): error C2105: '++' needs l-value
    c:\temp\c++\pec\pec\test.cpp(16): error C2105: '++' needs l-value
    Last edited by VanJay011379; 07-19-2002 at 04:41 PM.
    " . . . and I lay awake, big dreamers never sleep." - David Lee Roth

  2. #2
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    What kind of errors? Post them please.

  3. #3
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Note that 'origStr' and 'newStr' are const(arrays).
    You cannot make them point elsewhere.

    They will always point to the first element of the array.

  4. #4
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    I think the problem lies in the fact that your strings are declared as character arrays. This means the array identifier is a constant pointer to the first element which cannot be modified. This can be removed by using dynamic memory or declaring pointers to the arrays. eg

    Code:
    void main( int argc, char* argv )
    {
    	char* origStr = "alHello_0.shp";
    	char newStr[25];
    	char* ptr = newStr;
    
    	//strip the name down to the component
    	*newStr = *(origStr + 2);	//set the pointer in a to the third character of b
    	
    	//eliminate the rest of the characters from either the _ or the . char
    	for( ; (*origStr != '_') && (*origStr != '.'); origStr++, ptr++) 
    	{ *ptr = *origStr; }
    
    	++ptr;
    
    	//place a null at the present character
    	*ptr = '\0';
    
    	cout << newStr << endl;
    }

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Could someone tell me what is wrong?
    You have been tricked into thinking that arrays and pointers are always the same. This is not so and is often the cause for confusion among even seasoned programmers.

    -Prelude
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Jun 2002
    Posts
    59
    Originally posted by Prelude
    >Could someone tell me what is wrong?
    You have been tricked into thinking that arrays and pointers are always the same. This is not so and is often the cause for confusion among even seasoned programmers.

    -Prelude
    I always thought of them as similar but not quite the same.

    What is the technical difference? Does anybody have any good links or explanations with a compare/contrast?

    Is an array name a pointer?

    Thanks for the help
    " . . . and I lay awake, big dreamers never sleep." - David Lee Roth

  7. #7
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    This might have what you're looking for.

    >> Is an array name a pointer?
    Yes, it is. The only thing is that it is a constant pointer, so it will always point to the array.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer to global arithmetic operator?
    By m37h0d in forum C++ Programming
    Replies: 8
    Last Post: 01-30-2009, 08:40 PM
  2. simple pointer arithmetic
    By stanlvw in forum C Programming
    Replies: 1
    Last Post: 11-14-2007, 12:02 AM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 05:54 PM
  4. Arithmetic on pointer...?
    By Volair in forum C Programming
    Replies: 7
    Last Post: 11-22-2005, 01:58 PM
  5. Pointer arithmetic
    By freebu in forum C Programming
    Replies: 4
    Last Post: 04-27-2003, 10:27 AM

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