Recommended time: 30 minutes

Pointer

A pointer is a variable which stores some address. The address can be a memory address, which maps to other variables or codes. In the second year, you will know that pointer can also store hardware addresses, which aids software and hardware interaction.

Dereferencing operator(*):

int b = 0;
int* a = &b;
*a = 666;
// Since "a" stores the address of variable "b",
// dereferencing "a" and modify the value actually modifies "b"
// Therefore
printf("%d\n",b); // prints b, which is "666"
printf("%d\n",a); // prints a, which is the address of "b"
printf("%d\n",*a); // prints b, because (the address stored in a) is (b's address)

Strings

There are basically 2 ways to declare a string in C.

1. Using an Array

char myStringArr[256] = "Hello World!";

2. Using a char-type pointer

char * myStringPtr = (char*) malloc(sizeof(char)*256);
strcpy(myStringPtr,"Hello World!");

There is 1 common way to print a string

// As you may have known, both "myStringArr" and "myStringPtr" are pointers.
// You must always pass a pointer to printf() whenever you want to print a string
// Note: "myStringArr" points to the first element of the "myStringArr[256]" array

printf("%s\n",myStringArr);
// or 
printf("%s\n",myStringPtr);

Sample Question 1

As you probably know, the reverse of “20200202” is itself. This kind of strings is called “palindrome”.
Write a Boolean-type function to check whether a string is palindrome.

A function prototype is given as follows:

#include <stdio.h>
#include <stdbool.h>

// returns "true" if the given string is a palindrome.
bool isPalindrome(char* s, int sSize){

}

int main() {
    char myString[128] = "20200202";
    int mySSize = 8;
    bool result = isPalindrome(myString,mySSize);
    printf("Your function is %s\n", result?"Correct":"Wrong");
    return 0;
}

Try it yourself!


Junhao

Located in Markham, I am working as an Automotive Display Driver Engineer at Qualcomm Canada Inc. Previously, I graduated as a Computer Engineering undergraduate student at the University of Toronto and worked as an ECE297 TA there. As I once tutored ECE243 and APS105 at EngFastlane, now I am also providing tutoring service at TopLogic Inc.. I am proficient with C, C++, JavaScript and Python and familiar with PSQL, Java, Intel FPGA Verilog and ARM Assembly(V7). My interest is in Software Design and Development.

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *