C++ Learning Community Forum
August 01, 2010, 03:14:19 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Hello. Smiley
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: pls help i don't get strings  (Read 384 times)
solidgriever
N00b!!1
*
Posts: 7



View Profile
« on: March 06, 2010, 07:33:01 PM »

it's about index from strchr()
Code
GeSHi (c):
#include <stdio.h>
#include <string.h>
 
int main ()
{
char string1[] = "This is a test string";
char * character_pointer;
printf ("Looking for the 's' character in \"%s\"...\n",string1);
/* initialize character_pointer to the location of the first 's' in string1 */
character_pointer=strchr(string1,'s');
while (character_pointer!=NULL)
{
/* convert the pointer location to the position within string1 so it can be displayed */
        printf ("found at %d\n",character_pointer - string1 + 1);
/* increment the pointer and search for the next 's' */
        character_pointer = strchr(character_pointer + 1,'s');
}
return 0;
}
Created by GeSHI 1.0.7.18

i don't uderstand the "character_pointer - string1" part
if character_pointer is "s is a test string" and string1 "This is a test string" why (character_pointer - string1) is 4?
Edit1: i mean i know that strchr returns a pointer to the first 's' and the string continues till NULL, so (string1 - character_pointer) is -4 or ?  pointer arithmetic that i don't get?

Edit2: When you subtract two pointers, as long as they point into the same array, the result is the number of elements separating them. Is this it? so you can do this
Code:
if((string1 - character_pointer)<0) {
     (string1 - character_pointer) *= -1;}
instead of that counterintuitiv character_pointer - string1?
Edit3: i think i get it
  
  |00|01|02|03|04|05|06|07|08|09|10|11|12|13|14|15|16|17|18|19|20|          
  |T | h | i | s |   | i  | s|    |a  |   |t  |e  |s  |t  |   |s |t  |r   |i  |n  |g |\0
   ^            ^
|string1|  |character_pointer|
so ((character_pointer - string1) + 1) is ((3 - 0) + 1) Is that right?
« Last Edit: March 31, 2010, 12:38:25 PM by solidgriever » Logged
FrozenKnight
ASM Freak
Global Moderator
Dr. of C++ology
*****
Posts: 546


Do it yourself it's the only way to learn.


View Profile
« Reply #1 on: March 07, 2010, 03:48:43 PM »

I think you have the hang of it.
the expression you have in your last edit would be similar to using sizeof on the string segment.
The code you gave in your edit2 should produce a run-time error because you are trying to multiply the address 4 by -1, ( I believe that address 4 is reserved on most systems so this isn't a good thing)
Logged


Imagine the impossible, then make it happen.
solidgriever
N00b!!1
*
Posts: 7



View Profile
« Reply #2 on: March 07, 2010, 05:05:59 PM »

thx for the reply
i hit a wall on that code and was stuck... i just started learning C/C++ from a book
thx again FrozenKnight i can now move on
The code you gave in your edit2 should produce a run-time error because you are trying to multiply the address 4 by -1, ( I believe that address 4 is reserved on most systems so this isn't a good thing)
yeah, you're right that was stupid Tongue  
but if ai rewrite it like that:
Code
GeSHi (c):
#include <stdio.h>
#include <string.h>
int main (){
char string1[] = "This is a test string";
char * character_pointer;
int index;
printf ("Looking for the 's' character in \"%s\"...\n",string1);
character_pointer=strchr(string1,'s');
while ((character_pointer!=NULL) && (index = string1 - character_pointer))  {
     if (index < 0){
         index *= -1;}
         printf ("found at %d\n", index + 1);
         character_pointer = strchr((character_pointer) + 1,'s');
}
}
Created by GeSHI 1.0.7.18
if if multiply an int(i mean index) by -1 it's ok? altough string1 is string1[0] and character_pointer is string1[3] and inside if they are like int's
« Last Edit: March 31, 2010, 12:38:53 PM by solidgriever » Logged
FrozenKnight
ASM Freak
Global Moderator
Dr. of C++ology
*****
Posts: 546


Do it yourself it's the only way to learn.


View Profile
« Reply #3 on: April 03, 2010, 10:06:18 AM »

Ok, a few things.
the most obvious thing is
Code
GeSHi (c):
while ((character_pointer!=NULL) && (index = string1 - character_pointer))  {
Created by GeSHI 1.0.7.18
the "index = string1 - character_pointer" part isn't a Boolean expression and shouldn't be used with "&&"
I'm hoping you meant "index == string1 - character_pointer" but if you use that then you haven't initialized your index.

Second from reading your code i gather that you haven't realized that a string is the same thing as a character pointer
for example
Code
GeSHi (c):
char string[] = "This is a test string";
Created by GeSHI 1.0.7.18
and
Code
GeSHi (c):
char *string = "This is a test string";
Created by GeSHI 1.0.7.18
do exactly the same thing and on most compilers you can interchange the notation
example
Code
GeSHi (c):
return string[5];
Created by GeSHI 1.0.7.18
and
Code
GeSHi (c):
return *(string + 5);
Created by GeSHI 1.0.7.18
should be the same.

So this means that if your "string1 - character_pointer" returns a negative result then you have to handle an error condition not multiply it by -1.

Also as a general rule of thumb avoid using multiplication or division directly on pointers, the results probably won't be what you expect.
about the only time you should be using multiplication in/on/with a pointer is for cases like the following
Code
GeSHi (c):
int array[] = {0,1,2,3,4,5,6,7,8,9};
return *(array + (sizeof(int) * 5));
Created by GeSHI 1.0.7.18

Also most compilers handle bool like int's. The rules for a bool are simple if the bool (int) results to 0 then it's false if it's anything else it's true
so
Code
GeSHi (c):
if (-4356)
Created by GeSHI 1.0.7.18
is always true
if your above code works this is why, because "string1 - character_pointer" evaluates to a non-zero value then passes it to index. Because of a property of assignment where you can use "a = b = c;" this will pass the value that "string1 - character_pointer" returned to your "&&". I think you should test your code and see what happens when you have an 's' as the first character of string1, I'll bet it will fail by exiting the loop immediately without doing anything.

Also pointer sizes depend on the architecture your working on for example
if your working with an 8 bit compiler then a pointer will be 1 byte wide probably be the same size as a char (you probably wont ever work with one of these)
if it's a 16 bit compiler your pointers will be 2 bytes wide
if it's a 32 bit compiler it your pointers will be 4 bytes wide the same size as an int
This means in general you want to avoid on relining on a pointer being the same as an int, granted you can work with them that way, but I don't think anyone recommends it.
As for your index value i would consider it like a local pointer for use inside your string, so the rule for using multiplication on it should apply.
« Last Edit: April 03, 2010, 10:32:52 AM by FrozenKnight » Logged


Imagine the impossible, then make it happen.
solidgriever
N00b!!1
*
Posts: 7



View Profile
« Reply #4 on: April 03, 2010, 03:16:45 PM »

yeah i see what you mean
the original code was alright, but i didn't understood pointers and strings so in my attempt to make it easier to read.
i repaired my code below. now i get that the second condition wasn't a bool. i reviewed a few other codes i wrote and found the same mistake.
if i had put s as the first letter in my string then my second condition would fail because index would be 0.
about handling an error condition... i still havn't got to that part but i get why i don't need to multiply by -1 at least in this case. the character_pointer is incremented in every cycle so it will be greater then string1.

Code
GeSHi (c):
while (character_pointer)
{
          index =  character_pointer - string1;
             printf ("found at %d\n", index + 1);
         character_pointer = strchr((character_pointer) + 1,'s');
}
Created by GeSHI 1.0.7.18
Logged
FrozenKnight
ASM Freak
Global Moderator
Dr. of C++ology
*****
Posts: 546


Do it yourself it's the only way to learn.


View Profile
« Reply #5 on: April 04, 2010, 03:46:21 AM »

That code looks much better.

What i meant by handling an error condition, is simply that you should never have
Code
GeSHi (c):
index =  character_pointer - string1;
Created by GeSHI 1.0.7.18
result to a negative number, that would merely indicate that something went wrong. and in that case you should report it accordingly
So handling the error would look something like
Code
GeSHi (c):
while (character_pointer)
{
          index =  character_pointer - string1;
          if (index < 0)
          {
             printf ("ERROR: Charicter search failed\n"); /* print error */
             break; /* break from loop because, if we hit this then the loop may continue infinitely or cause an error later on */
          }
          printf ("found at %d\n", index + 1);
          character_pointer = strchr((character_pointer) + 1,'s');
}
Created by GeSHI 1.0.7.18
Handling errors using methods similar to this is good practice in general. Because, you never know when your going to run into an error, and reporting it helps you track them down.
« Last Edit: April 04, 2010, 03:56:20 AM by FrozenKnight » Logged


Imagine the impossible, then make it happen.
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!