Thread: i need help with this function, I can't get sizeof(char) to work properly

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    33

    i need help with this function, I can't get sizeof(char) to work properly

    this code works like this

    lets say we have a string word' = "abcdabc";

    and we want to erase a word that is included in word'

    if word = "abc" we want to make word' = " d ";

    i ve done this so far

    Code:
    #include <stdio.h>
    #include <string.h>
    
    void str_return(char input[],char strsearch[]){
         
         int i,z;
       //  printf("%s\n",input);
       //  printf("%s\n",strsearch);
        //getchar();
    
             for (i=0;i<14;i++){
        
              if (&input[i] == strstr(input,strsearch) && strstr(input,strsearch)!=NULL){
                        
                        for (z=i;z<i+strlen(strsearch);z++){
                        input[z] = ' ';
                        }
                        
                        }
    
                }
    
         }
    int main(void){
        
        char x[] = "lolsopaasdlol";
        int i=0,y=0,z;
        printf("%d\n",sizeof(x)/sizeof(char));
        str_return(x,"asd");
        
        printf("%s\n",x);
         
             // system("pause");
        
        return 0;
        
    }
    here -> for (i=0;i<14;i++){

    i want to put where 14 sizeof(input)/sizeof(char)

    but the sizeof(input)/sizeof(char) inside the function is 4 while in main() function it's 14

    why is that?

    Thanks
    Last edited by jackhasf; 12-09-2009 at 11:51 AM.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by jackhasf View Post
    why is that?
    Because x is a char array, and "input" is a char* (you should use char *input in the param list, it means the same thing and may be less confusing for you).

    Use strlen+1 instead.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    33
    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM