-3

I found this program on-line, that claims to split a string on the format "firstName/lastName". I tested, and it works:

char *splitString(char* ptrS, char c){ 
    while( *ptrS != c ){   
        if( *ptrS == '\0'){
            return NULL;
        }      
        ptrS++;
    }   
    return ptrS;
}

int main(){

    char word[100];
    char* firstName;
    char* lastName;

    printf("Please insert a word : ");
    fgets(word, sizeof(word), stdin);
    word[strlen(word)-1] = '\0';

    firstName = word;   
    lastName = splitString(word, '/');    
    if(lastName == NULL ){
        printf("Error: / not found in string\n");
        exit(-1);
    }

    *lastName = '\0';   
    lastName++; 
    printf("First name %s, Last name %s\n",firstName, lastName);
    return(0);  
}

What I'm seeing here however, is only one char array being created, and the firstName and lastName pointers being placed properly.Maybe it is because I'm a little confused about the relation between pointers and arrays but I have a few questions about this program:

  • How many strings -as char arrays- are produced after the program is executed?
  • Are the char pointers used in this program the same as strings?
  • Why I can use those char pointers used as strings in printf? I can use char pointers as strings on every C program?

As a corollary, what's the relationship between pointers and arrays? Can they be used interchangeably?

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • 3
    Do you know the definition of a string in C? It's better not to ask so many questions together, and it's easy to find answers to at least some of your questions if you google a little. – Yu Hao Oct 20 '14 at 13:34

2 Answers2

2

How many strings -as char arrays- are produced after the program is executed?
You have one char[], of size 100. However, at the end of the program's execution, you actually have two strings stored in it.

A string is a set of chars stored contiguously in memory terminated with '\0' (null/0).
In this program we take the input, and search for the '/' character. When found, it is replaced with the string terminator:

"John/Smith\0"
"John\0Smith\0"

The pointer to the beginning of the array will allow us to access the first string ("John"), and the pointer that previously pointed to the '/' character is incremented, so that it now points to "Smith"

initialisation:

"John/Smith\0"
 ^     
 |     
 word/firstname

after lastName = splitString(word, '/');:

"John/Smith\0"
 ^   ^
 |   lastname
word/firstname

after *lastName = '\0'; lastName++;:

"John\0Smith\0"
 ^     ^
 |     lastname
 word/firstname

So there is still only one array (word) but you have two pointers (firstname &lastname) to the strings within.

Are the char pointers used in this program the same as strings?
The char* pointers are not strings, but they point to them. You can pass the pointers to any function that is expecting a string (I expand a bit below)

Why I can use those char pointers used as strings in printf? I can use char pointers as strings on every C program?
In C (and C++) functions cannot pass arrays of data. A function like this:

int func(char string[]){}

will not actually be passed the array itself, but instead, it will be passed a pointer to the beginning of the array.
It is often the case that the [] notation is used where the function will be operating on an array (in function headers char* str and char str[] are equivalent) as it removes any ambiguity that the pointer isn't there to reference a single char (or int etc).
Printf() takes pointers to the string, and knowing that it is a string (thanks to the %s identifier in the format string) it will print out each char from the memory location that the pointer identifies until it hits the '\0' character.
A valid string always has this terminator, and so when working with strings, this is safe.
You will often see functions that take an array, and an additional parameter to denote the size of the array:

int do_something_with_array(int array[], size_t array_length){}

This is because without a terminating value or knowing the size of the array, there is no way to know that you have left the array and started processing out of bounds which isn't allowed and can cause memory corruption or cause the runtime to kill your program (crash).

It is a common misconception that pointers and arrays are the same.
In most cases you handle arrays via pointers, so that handling the data is the same whether you have an array or a pointer to one. There are some things to keep in mind however; this as an example.

Community
  • 1
  • 1
Baldrickk
  • 4,291
  • 1
  • 15
  • 27
1

How many strings -as char arrays- are produced after the program is executed?

There are 1 char array in this program, namely word[100], others like firstName and last Name are just char pointers..

Are the char pointers used in this program the same as strings?

Char pointers are very different to Strings, Char pointers are something you can use to point at a char or a string. In this program firstName and lastName are only char pointers that are used to point at different places of the character array word[] to split the array

Etheryte
  • 24,589
  • 11
  • 71
  • 116
Haris
  • 12,120
  • 6
  • 43
  • 70