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

I work at YScope, a company known for providing innovative log management and troubleshooting tools for software systems. Prior to this, I worked as an Automotive Display Software Engineer at Qualcomm Canada ULC in Markham, Canada. I am proficient with C, C++, ARM Assembly(V7), and Intel Verilog HDL, and I also have a good command of Python, JavaScript, PSQL, Java, and Go-lang. My interests lie in Embedded/Web Software Design, Development, and Integration. I developed iCtrl, a simple web application for remote graphical / terminal connections and file transfers, which has garnered over 52,000 downloads as of August 2023. Previously, I graduated as a Computer Engineering undergraduate student at the University of Toronto and worked as an undergraduate ECE297 TA there.

0 条评论

发表评论

Avatar placeholder

电子邮件地址不会被公开。 必填项已用*标注