Today we will look at arrays and strings. The short description is that C requires you to be more explicit with how much memory you need for an array, but after that, pointers and array usage is the same.
| C++ | C |
|
|
In C, we instead use the keywords malloc or
calloc. They're almost, but not exactly, the same.
malloc is:
void* malloc(int);
#include <stdlib.h> to
use malloc and calloc.
int needs to be the number of bytes
that you want for your array from the operating system.
When requesting space for an array of 5 ints, for example, to arrive at the correct number of bytes to request:
Most systems represent an int with 4 bytes, but not always, and embedded systems
can be different from platforms. To know how many bytes your type needs, use
the sizeof() function.
sizeof(int) returns the number of bytes needed to
store a single int. This is then multiplied by 5 (because you need room for 5
Because malloc just accepts the number of raw bytes to be
allocated, it has no knowledge of the type you're going to store within that space, so it
doesn't know whether to return a int* or a char* or
what. So, it just returns a void*.
This void pointer can be cast to any of
those types. So, for our array of 5 ints, we finally end up at:
int* intArr = malloc( 5*sizeof(int) );
Say we instead want 12 doubles, then it looks like this:
double* doubles = malloc( 12*sizeof(double) );
| C++ | C |
|
|
calloc has two input parameters.
calloc zeroes it
out the allocated memory for you!
The choice between malloc
and calloc is largely a personal preference between the two, as
long as you're knowledgeable about these differences.
Remember that scanf requires a pointer to know which memory address will be
filled. For example, reading an int:
int* nums = malloc( 10*sizeof(int) );
for( int i = 0; i < 10; i++ )
scanf("%d", &nums[i]); // we just need the & to get the address of the array cell
Remember to delete your memory allocation when you're finished with your arrays!
The C equivalent of delete is the function free:
free(intArr);
free(doubles);
So, if you were to encode the word "hi" as a C string, you would need an array of size three:
char str[3];
str[0] = 'h';
str[1] = 'i';
str[2] = 0; //alternatively, the character '\0'
print("%s\n", str);
strcpy function.
The prototype is as follows:
char* strcpy(char* s_to, const char* s_from);This function copies string
s_from to s_to. For
conveniece, the function returns s_to as well.
Sample code:
|
|
strcmp function.
The prototype is as follows:
int strcmp(const char* a, const char* b);This function compares strings
a and b.
strlen function.
Sample code:
|
| $ ./a.out two strings: alice bob alice < bob length of s1=5 $ ./a.out two strings: bob alice bob > alice length of s1=3 $ ./a.out two strings: alice alice alice = alice length of s1=5 |
data.txt containing strings and prints
them out in reverse and the maximum string.
For example, consider the following file data.txt
8 this function returns a length of string s
$ ./a.out
s
string
of
a
length
returns
function
this
maximum = this
Solution
$ ./a.out
How many? 5
5 3 9 2 3
3 2 9 3 5
$ ./fileread
Average is 34.850000
Max is 94
$ ./name
Alan Turing
TURING, ALAN
See a program solution.