Thread: Difference between Char* X and Char *X

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    29

    Difference between Char* X and Char *X

    What is the difference between a Char* X and a Char *X. In my code for homework we were given a Char* X, and when I tried to access it like a pointed to value (i.e.if *X=='y'), it complained about there being a pointer expected.

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    There's no difference between "char *x" and "char* x", this is syntactically equivalent for the compiler, the style is up to you.
    There must be another difference you didn't spot, post your code.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    29
    Code:
    int CountCharacters(char* String, char Character)
    {
      	int n=sizeof(*String);
     	int count=0;	
    	int i; 	
    	for (i=0; i<n; i++){
    		if (*String[i]==Character){
    			count++;
    		}
    	}
    	return count;
     
    
    }

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Your problem is in the following line:
    Code:
    if (*String[i]==Character){
    When you use array notation[] on your variable you don't use pointer notation * at the same time. So delete the * and you should be okay.

    Also your sizeof() call is not giving you the number of characters in your string. Try printing out the value of n after this call. You may want to pass the size of your string into your function.

    Jim
    Last edited by jimblumberg; 08-20-2012 at 01:59 PM.

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by cloudsword View Post
    Code:
    int CountCharacters(char* String, char Character)
    {
      	int n=sizeof(*String);
     	int count=0;	
    	int i; 	
    	for (i=0; i<n; i++){
    		if (*String[i]==Character){
    			count++;
    		}
    	}
    	return count;
     
    
    }
    Use strlen() instead of sizeof(), and drop the * in the if *String[i] part. The reason is that the array indexing into String handles de-reference for you.

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    Okay.
    "String" is a pointer to a character, a string in C is an array of characters, terminated by the character \0.
    So if you do "*String", you get the first character of the string.
    If you do "String[i]", you get the Ith character of the string.
    "*String[i]" makes no sense in your case, that's why the compiler complains, I let you guess how to fix it.
    At last the function to get the length of a string is strlen(), not sizeof() and certainly not with *String once again because you're only taking into account a single character.
    I suggest you review how strings works in C to be sure everything's clear.
    Last edited by root4; 08-20-2012 at 02:02 PM.

  7. #7
    Registered User
    Join Date
    Sep 2009
    Posts
    29
    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Difference between char * and char [] arguments?
    By stevenswj in forum C Programming
    Replies: 4
    Last Post: 11-28-2010, 08:36 AM
  2. Difference between char *str1 & const char *str2
    By Tigers! in forum C Programming
    Replies: 4
    Last Post: 08-06-2009, 04:04 AM
  3. Replies: 16
    Last Post: 04-01-2008, 10:15 AM
  4. Difference between char x[n] and char* x=new char[n]
    By _izua_ in forum C++ Programming
    Replies: 7
    Last Post: 08-10-2007, 07:18 PM
  5. difference between: const char *, char *
    By creeping_death in forum C Programming
    Replies: 3
    Last Post: 08-04-2003, 11:51 PM

Tags for this Thread