Thread: compare between a array of character and a pointer to an array of characters

  1. #1
    UK2
    Join Date
    Sep 2003
    Posts
    112

    compare between a array of character and a pointer to an array of characters

    Hello,

    I am just wondering if how can you compare between these two arrays.

    Code:
    char *string1 = "Hello how are you?";
    char string[] = "hello how are you?";
    
    if(string1 is a pointer to a string of characters)
    {
        //do something here
    }
    else
    {
       //this is an array so do something else.
    }
    Many thanks,

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Yes, use strcmp. EG:
    Code:
    if( strcmp(string, string1) == 0)
        // Strings match

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    7
    maybe using sizeof():

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
         char *string1 = "Hello how are you?";
         char string[] = "hello how are you?";
    
         printf("sizeof string1: &#37;d\n", sizeof(string1));
         printf("sizeof string: %d\n", sizeof(string));
    
         return 0;
    }
    So the test would be like that:

    Code:
    if (sizeof(string) == sizeof(char*))
    {
       //pointer
    } else
    {
       //array
    }
    However there is still the special case where your array contains 4 characters...

  4. #4
    UK2
    Join Date
    Sep 2003
    Posts
    112
    Yes that would be correct.

    I don't want to compare the contents of the arrays.

    But to compare if one is a pointer to array and the other is a array.

    Thanks,

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    But why do you want to see if it's a pointer to an array or merely an array? Both can be treated much the same (especially if it's const char*).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Code:
         char *string1 = "Hello how are you?";
         char string[] = "hello how are you?";
         char *string3 = malloc( 100 ); strcpy( string3, "Hello how are you?" );
         foo( string1 );
         foo( string );
         foo( string3 );
    As far as foo() is concerned, there is nothing to tell them apart. There may be some very system specific things you can do, but there is nothing portable.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    UK2
    Join Date
    Sep 2003
    Posts
    112
    Hello,

    The reason I was asking if you could compare if the arrays implementation are different was because I was returning from a function.

    This might be better explained with my code below. if you are return a *string or a string[].

    The idea is to convert the whole string to upper case without depending on any other libraries.

    However, if the user passes in a *string or a string[] would it depend on how I return them to the calling function. This is what I am experimenting with here.

    I thought if I new what was being used, I could return based on that.

    Many thanks,


    Code:
    nt main(int argc, char** argv)
    {
    	char *string1 = "Hello how are you today?";
    	char string2[] = "Hello how are you today?";
    
    	string1 = ToUpper(string1, strlen(string1));
            free(string1);
    	printf("string1: &#37;s\n", string1);
    	string2[0] = ToUpper(string2, strlen(string2));
            free(string2);
    	printf("string2: %s\n", string2);
    
    	return 0;
    }
    
    
    char* ToUpper(char *str, const int length)
    {
    	char *reset = str; //For reseting the pointer to point to the ordinal address.
    	int character;
    	char *destString = malloc(length * sizeof(char));
    	int i = 0;
    
    	//Check to see if actual memory was allocated.
    	if(destString == NULL)
    	{
    		return NULL;
    	}
    
    	//Copy the contents to the array. 
    	//A pointer to an array cannot be modified
    	while(*str)
    	{
    		destString[i] = *str++;
    		i++;
    	}
    	destString[i] = '\0'; //Terminate the end of the string.
    
    	str = reset; //Reset the pointer to point to the first character in the character array.
    
    	i = 0;
    	while(*str)
    	{
    		//Get the character and subtract 32 to make it upper case.
    		//Filter for only lower case characters.
    		if(*str >= 97 && *str <= 122)
    		{	
    			//Get the character that is lower case and subtrack 32 to make it a upper case character
    			character = (int) destString[i];
    			character -= 32;
    			//Assign the upper case character back into its position in the array.
    			destString[i] = (char) character;
    		}	
    		str++;	
    		i++;
    	}
    
    	destString[i] = '\0';
    	//Point the character array about to the first character in the array.
    	//str = reset; 
    	str = destString;
    
    	return destString;
    }
    
    
    int main(int argc, char** argv)
    {
    	char *string1 = "Hello how are you today?";
    	char string2[] = "Hello how are you today?";
    
    	string1 = ToUpper(string1, strlen(string1));
    	printf("string1: %s\n", string1);
    	string2[0] = ToUpper(string2, strlen(string2));
    	printf("string2: %s\n", string2);
    
    	return 0;
    }

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I think you are confused.
    Code:
    char* mystr = "my string";
    Is a string literal (which should be const char*, btw) and should never be freed. ONLY a pointer from malloc should be passed to free.
    Further, if you pass an array to a function, it will pass a pointer to the first element anyway, so you still get a pointer.
    The caller should free the memory if appropriate, or otherwise, pass a bool indicating if the memory should be freed to the function.

    And... don't use "magic" values. Use something like if (something >= 'A'), etc.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed