Xiaobai Road---C Language

Many friends will encounter some algorithm questions when learning C language. Today, I will write a simple algorithm, an interview question, and some knowledge points I encountered in my study yesterday.

  1. Given an array, you are required to find out which element in the array is unique. (I will write my own understanding. If you are interested, you can write the code.)
    First of all, when you see the topic, it is said to find a unique number for an array, then can we think that this so-called unique number, he Appeared once in the array? If this is the case, it's easy to do, do two loops, each time you loop, you must compare whether it is equal to the first one.
    1) Declare two initialization variables (due to the C99 for loop standard, you cannot initialize the variables when you use them)
    2) To find the size of this array, you can use the sizeof operator to find the memory of the array. The
    basic principle is:
    sizeof(array name)/sizeof(array[0]) ------ total memory size/single memory size
    3) As you can see in the previous step, we have got the number of arrays, then we can start the loop ,
    4) How do we know whether these two numbers are equal, or how many times do they appear? At this time, a timer is needed.
    Every time the two elements judged by the loop are equal, the timer++ can be used to find out how many times the element appears in the array. This looks like a bubble sorting method.
    5) When the timer is equal to 1, it means that the current element appears once in the array.
    6) Finally, after the first loop, the second loop variable must be cleared, otherwise it will keep accumulating.

2. Next, let's talk about an interview question: when the third variable is not used, if the values ​​of the a and b variables are exchanged.

    1).首先看到这个题,我们可以立马想到,如果使用第三个变量的时候,这个题怎么解决呢?
            假设,你有三个瓶子,把第一瓶子的东西放到,第三个瓶子里,  第二个瓶子的内存放到第一个瓶子里,  第三个瓶子的内容,放到第二个瓶子里,这样就可以实现 两个变量开始交换了。
    2).那么这个题说不使用第三个变量,其实我们可以这样做。
            还是假设,两个数字 a = 10;  b = 5;
                    1).a = a + b;   那么此时的a就是这两个数的和
                    2).b = a - b ;   总和-b的值,然后重新赋值给b  此时的b就是 10
                    3).a = a - b;   同样的原理,这里就不说啦,
     3).那么这么简单,你就会问了,这个我也会,这里要提前说一下抱歉,因为面试官不是考你这么简单的问题,同时 他还会问你,这个有没有提升的可能性
            我们都知道,整型变量都会自己的内存大小的,超过内存大小的话,就会造成内存溢出,那么就这道题来说,如果解决溢出的问题呢?可以用逻辑异或的想法来做。
            a = a^b;
            b = a^b;
            a = a^b;
            这是以二进制来进行逻辑异或的,相同为0 ,相异为1;

3. Then let me talk about the knowledge points that I think I encountered in my study yesterday,

    1).外边引用变量  需要extern 关键字 在使用的时候需要声明他的类型,和变量名
    2).static修饰局部变量的时候,会把他的声明周期变长。
    3).static修饰全局变量的时候,会把他的作用域变成本文件内, 也就是说把 外部链接属性--->内部链接属性
    4).typedef 类型重新定义  typedef  int  a,那么下边就可以直接a来定义变量的类型了。

4. Shallow pointers

    1).我们都知道指针是C语言中中的一大难点,今天我就来跟大家谈一谈我对指针的 简单看法,(这只会说到指针简单的应用,并不会说太深,下篇博客会着重写指针);
            在你的脑袋里想一下哈,比如你要找  xxx大学的 xxx系xxx班xxx学生,应该那么找勒
是不是要先找到学校,系部,班级,还有学生名呢?  是不是想到这里,就有了一个地址的概念。
            我们都知道一个字节是8个比特位,四个字节是整型变量能存放的最大的内存单元,
                        当我们 int  a = 10; 是不是就意味的像内存申请了内存空间,
    ,说到这里我就想问了,内存空间的地址是什么呢?  大家肯定都晓得撒,计算机只能识别二进制,0111010,从全0到全1,这类的数字,所以内存地址的编址方式,就是由这些比特位来组成的
        那有多大呢?  一共是 2^32次方大小的地址,换算成十进制,很大大了,所以说整型变量还是很大的,
    2).简单说一下什么是指针变量和解引用操作符
                指针变量----存放一个变量地址的变量,
               int  c = 10;
                 int* a = &c;

                 *a = 20;
                 请问当前的c是多大?     解引用操作符是  本身存放的是变量c的地址,但是后边复制的元素会改变指向地址的值得。

                (宏定义,这篇就不说了,定义常量会在下篇说)

Guess you like

Origin blog.51cto.com/15121467/2649984