ASIC Verification: C Strings

Tuesday, February 26, 2008

C Strings

You must become familiar with the pointer concepts to use C strings effectively. Once you get used to them, you can often perform string manipulations more efficiently.

A string in C is simply an array of characters. The following line declares an array that can hold a string of up to 99 characters.

char str[100];

str[0] is the first character of the string, str[1] is the second character, and so on. But why is a 100-element array unable to hold up to 100 characters? Because C uses null-terminated strings, which means that the end of any string is marked by the ASCII value 0 (the null character), which is also represented in C as '\0'.

The following is a representation of what would be in RAM, if the string "Hello, world!" is stored in this array.

characters : H e l l o , w o r l d !
Hex values : 48 65 6C 6c 6F 2C 20 77 6F 71 6C 64 21 00
Subscripts : 0 1 2 3 4 5 6 7 8 9 10 11 12 13

The name of the array is treated as a pointer to the array. The subscript serves as the offset into the array, i.e., the number of bytes from the starting memory location of the array. Thus, both of the following will save the address of the 0th character in the pointer variable ptr.

ptr = str;
ptr = &str[0];

In what way are character arrays different than numeric arrays?
Can elements in a character array be accessed in the same way as the elements of a numeric array?
Do I need to take any special care of '\0'?
Why numeric arrays don't end with a '0'?
/*program to demonstrate printing of a string*/
char name[ ] = "Suresh"; /*initialized a character array*/
int i=0;
printf("%c", name[i]);
} // End of while
} // End of main()
can we write the while loop without using the final 5? We can. Because each character array always
ends with a '\0'.
char name[ ] = "Suresh"; /*initialized a character array*/
int i=0;
while(name[i] != '\0')
printf("%c", name[i]);
} // End of loop
} // End of main()

Accessing array elements using the Pointers:
main() {
char name[ ] = "Suresh"; /*initialized a character array*/
char *ptr;
ptr = name;
/* store base address*/
while(*ptr != '\0')
printf("%c", *ptr);

// End of loop
} // End of main()
In fact, the character array elements are accessed in the same way as the elements of an integer array.

More on Strings

The length of the string should not exceed the dimension of the character array. This is because C compiler doesn't perform bound checking.

scanf() is not capable of receiving multi-word strings. Therefore, names such as Suresh Chellappa would be unacceptable. The way to get around the problem is by using the function gets().
char name[25];
printf("Enter your full name\n");


Two dimensional array of characters

Now let us look at an example for 2-D array of characters:
#define FOUND 1
#define NOTFOUND 0
char master_list[5][10] =
{ "ASIC", "FPGA", "DFT", "STA","PANDR"};

int i, flag, a;
char yourfield[10];
printf("Enter your Field\n");
scanf("%s", &yourfield);
flag = NOTFOUND;
for(i=0; i<= 4; i++)
a = strcmp(&master_list[i][0], yourfield);
if (a==0)
printf("Your filed is related to ASIC\n");
flag = FOUND;
} // End of if condition
} // End of for loop
if(flag == NOTFOUND)
printf("Sorry man\n");


Syntax: len = strlen(ptr); where len is an integer and ptr is a pointer to char.

strlen() returns the length of a string, excluding the null. The following code will result in len having the value 13.

int len;
char str[15];
strcpy(str, "Hello, world!");
len = strlen(str);


Syntax: strcpy(ptr1, ptr2); where ptr1 and ptr2 are pointers to char.

strcpy() is used to copy a null-terminated string into a variable. Given the following declarations, several things are possible.

char S[25];
char D[25];
  • Putting text into a string:
    strcpy(S, "This is String 1.");
  • Copying a whole string from S to D:
    strcpy(D, S); 

Syntax: strcat(ptr1, ptr2); where ptr1 and ptr2 are pointers to char.

strcat() is used to concatenate a null-terminated string to end of another string variable. This is equivalent to pasting one string onto the end of another, overwriting the null terminator. There is only one common use for strcat().

char S[25] = "world!";
char D[25] = "Hello, ";
  • Concatenating the whole string S onto D:
    strcat(D, S);

Syntax: diff = strcmp(ptr1, ptr2); where diff is an integer and ptr1 and ptr2 are pointers to char.

strcmp() is used to compare two strings. The strings are compared character by character starting at the characters pointed at by the two pointers. If the strings are identical, the integer value zero (0) is returned. As soon as a difference is found, the comparison is halted and if the ASCII value at the point of difference in the first string is less than that in the second (e.g. 'a' 0x61 vs. 'e' 0x65) a negative value is returned; otherwise, a positive value is returned. Examine the following examples.

char s1[25] = "pat";
char s2[25] = "pet";

diff will have a negative value after the following statement is executed.

diff = strcmp(s1, s2);

diff will have a positive value after the following statement is executed.

diff = strcmp(s2, s1);

diff will have a value of zero (0) after the execution of the following statement, which compares s1 with itself.

diff = strcmp(s1, s1);


What is the difference b/w strcpy and memcpy? When should each be used?
How can I convert a string into number?
How can I convert a number into string?
How do you print only the part of the string?
How do I know, how many elements an array can hold?

No comments: