C Strings & Pointers

This is where members can submit tutorials that they have created on any computing related subject.
Post Reply
User avatar
worm
Sargeant at Arms
Posts: 251
Joined: Sun Apr 11, 2004 7:32 pm

C Strings & Pointers

Post by worm » Mon Feb 21, 2005 3:05 pm

I decided to compose this tutorial due to ennui, and with hopes of acquiring more apprehension on the subject. This tutorial will consist of working with C's Strings and Pointers.

First and foremost, I will manifest one of the prerequisites of Strings and pointers, Arrays, but before I commence to spiel the technical details, I want to point out the purposive of them. One: they are useful for lists of information, such as telephone numbers, grade lists etc.. Two: You need arrays in C in order to use strings, because C has no built-in string data type. Now that you have been briefed, we shall move on.

Arrays are a list of variables of the equivalent data type that are seperated by what you call "array elements". To declare an array, use the form of

Code: Select all

type var_name[size];
With type being a data type. var_name being the name of the array, and size being the size of the array. e.g.

Code: Select all

int arrayi[20]; 
Now that we have become acquainted with declaring arrays, lets see if we can fathom manipulating them. I want to start by vocalizing the certitude that you can't assign one entire array into another.

e.g.

Code: Select all

int array1[20], array2[20];
 
     array1 = array2; /* THIS IS WRONG */

If you desire to copy one array to the other, it's essential that you copy it element by element. To copy element by element, you could dissipate your time and do

Code: Select all

array1[0] = array2[0];
 array1[1] = array2[1];
 array1[2] = array2[2];
 ....and so on.
Or you could be an astute individual and use a loop to automate the process, like so

Code: Select all

 int i;
  
  for(i = 0; i < 20; i++) array1[i] = array2[i];
Now I would like to raise an issue on arrays, they use 0 based indexing. Which means that instead of counting 1. 2. 3, you will count 0. 1. 2. etc..


This is all that is imperative to know about arrays in regards to strings.

Strings
-------

First I want to properly delineate a string. A string in C is a null-terminated character array. Null being equivalent to 0. So when declaring a string array it is necessary that you assign it to be one byte larger than the string.

Due to a string being a null-terminated character array, you declare it like so

Code: Select all

char var_name[size];
There are multiple ways to read in strings. I am only going to discuss one. I will probably be yelled at for it, but whatever.

The method we will use to read in strings is the gets() function which is in the "stdio.h" header file. When you read in a string using the gets() function, you simply supply the name of the character array, without the index.

e.g.

Code: Select all

char string[80];

     gets(string);
There are two ways that you can print the string to the terminal. One you can use the string variable as an argument to printf, like so

Code: Select all

printf(string);
However, that only prints the string to the screen and nothing more, if you wanted to say add a carriage return or add some more text to the string you should use printf's %s format specifier, like so

Code: Select all

printf("%s\n", string);
Now I want to talk about a couple of important string-related functions.

strlen() - returns the length, in characters, of a string. strlen() does not count the null terminator.

syntax strlen(string);

strcpy() - copies contents into variable.

syntax strcpy(to, from);

strcat() - adds contents of one string to another.

syntax strcat(to, from);

strcmp() - compares to strings.

syntax strcmp(string1, string2);

strcmp() returns 0 if the strings are the same. It returns less than 0 if string1 is less than string2. It returns greater than 0 is string1 is greater than string2.

One string is not greater than the other because it has more characters, strcmp() compares strings lexicographically, which means in dictionary order. Therefore, a string is less than another when it would appear before the other in a dictionary, and greater than when it appears after the other. The comparison is also case sensitive, lowercase being greater than uppercase.

These string-related functions are found in the "string.h" header file.

Pointers
----------

Ahh, C's most important, but also most troublesome feature. Pointers. A pointer is a variable that holds the memory address of another object.
e.g. If a variable called point1, contains the memory address of a variable named point2. It is said that point1 points to point2.

To declare a pointer, use the form of

Code: Select all

type *var_name
Type is the base type of the pointer. Base type specifies the type of object that the pointer can point to.

e.g.

Code: Select all

 int *point
Creates a pointer to an integer.

C contains two special pointer operators, * and &. The & operator returns the address of the variable it precedes. The * operator returns the value stored at the address that it precedes.

Well I was expecting to write more about pointers, but I am too ridiculously lazy. Maybe I will create another tutorial that goes more in depth. Please excuse errors, this was a 20 minute tutorial :p
Will we let ignorance and laziness bring our demise?

User avatar
NoUse
time traveller
Posts: 2624
Joined: Thu Aug 28, 2003 10:46 pm
Location: /pr0n/fat

Post by NoUse » Mon Feb 21, 2005 7:29 pm

Never use gets(), bad function. You knew you were going to get yelled at, so why didn't you use a diff way for input?
And I will strike down upon thee with great vengeance and furious anger
those who would attempt to poison and destroy my brothers.
And you will know my name is the Lord
when I lay my vengeance upon thee.

User avatar
B-Con
Challenge Winner [1x]
Posts: 2679
Joined: Thu Apr 22, 2004 4:19 pm
Location: UC Davis
Contact:

Post by B-Con » Tue Feb 22, 2005 2:58 am

NoUse is absolutely correct......

http://sol-biotech.com/code/SecProgFAQ.html

read up on improved string manipulation
- "Cryptographically secure linear feedback shift register based stream ciphers" -- a phrase that'll get any party started.

- Why know the ordinary when you can understand the extraordinary?

User avatar
Prism
Owns you
Posts: 1618
Joined: Thu May 06, 2004 9:18 am

Post by Prism » Tue Feb 22, 2005 12:40 pm

you shouldn't be using strcat or strcpy either, instead use strncat and strncpy, and instead of gets use fgets

User avatar
worm
Sargeant at Arms
Posts: 251
Joined: Sun Apr 11, 2004 7:32 pm

Post by worm » Sat Feb 26, 2005 8:05 pm

NoUse wrote:Never use gets(), bad function. You knew you were going to get yelled at, so why didn't you use a diff way for input?
My intentions were for the readers to grasp the concepts of working with strings, and at a later time, they could read up on the other
Will we let ignorance and laziness bring our demise?

H@24rd
Banned
Posts: 13
Joined: Sun Jun 12, 2005 2:12 am
Location: My Box, and maybe yours...
Contact:

Post by H@24rd » Sun Jun 12, 2005 3:46 am

why is everyone getting all butt-hurt over him using the function gets() i mean, who really cares... oh, obviously you guys do, lol.

Sorry, If I was rude.

By the way, I think you did a nice job, worm!


Choking and gagging,
Hazard
Image removed by Motz. Read the rules.
Handles:
Noodles,
Hazard,
My IQ: 118
My age: 12

(\_/)
(O.o)
(> <) This is Bunny. Copy Bunny into your signature to help him on his way to world domination.

User avatar
B-Con
Challenge Winner [1x]
Posts: 2679
Joined: Thu Apr 22, 2004 4:19 pm
Location: UC Davis
Contact:

Post by B-Con » Sun Jun 12, 2005 4:55 am

why is everyone getting all butt-hurt over him using the function gets() i mean, who really cares... oh, obviously you guys do, lol.
because it's insecure, read the link I provided above
- "Cryptographically secure linear feedback shift register based stream ciphers" -- a phrase that'll get any party started.

- Why know the ordinary when you can understand the extraordinary?

User avatar
DevianT
n00b
Posts: 2
Joined: Mon Jun 27, 2005 5:51 am
Location: /home/deviant
Contact:

C

Post by DevianT » Tue Jun 28, 2005 4:37 am

I've been wondering what the main differences in programming languages are. I learned some C, and would like to know what perl, etc do differently than c/c++...thanks.

-- DevianT
We've been dominated by sadists, or
ignored by the apathetic. The few that had something to teach found us will-
ing pupils, but those few are like drops of water in the desert. -- The Mentor

User avatar
Net Battle Bot
Owns you
Posts: 1816
Joined: Fri Jun 04, 2004 6:44 am
Location: Groom Lake

Post by Net Battle Bot » Tue Jun 28, 2005 4:54 am

DevianT wrote:I've been wondering what the main differences in programming languages are. I learned some C, and would like to know what perl, etc do differently than c/c++...thanks.

-- DevianT
This is not the right place to post questions on languages. This is a place to comment on people's tutorials. There is a programming forum if you scroll down the page - ask programming-related questions there.
Without practice one cannot prove; without proof one cannot be trusted; without trust one cannot be respected.

User avatar
AcidMeister
Corporal
Posts: 144
Joined: Sat Mar 12, 2005 12:37 am
Contact:

Post by AcidMeister » Tue Jun 28, 2005 11:27 pm

worm wrote:
NoUse wrote:Never use gets(), bad function. You knew you were going to get yelled at, so why didn't you use a diff way for input?
My intentions were for the readers to grasp the concepts of working with strings, and at a later time, they could read up on the other
If you teach good security from the beginning, it leaves less room for insecurity later....
The minimum requirements stated "Windows 98 or better" So naturally, I installed FreeBSD

User avatar
hotx
Hacker in Training
Posts: 78
Joined: Thu Jun 30, 2005 4:08 pm
Location: Michigan
Contact:

Post by hotx » Thu Jun 30, 2005 4:13 pm

Worm, great tutorial. However, AcidMeister is correct. Being security concious in the beginning will sure save a lot of nightmares in the end.

SudoX
n00b
Posts: 2
Joined: Fri Jul 08, 2005 3:34 pm

Post by SudoX » Fri Jul 08, 2005 4:08 pm

Nice Tut

Post Reply