/*================================================================
* Copyright (C) 2021 HSW_study Ltd. All rights reserved.
*
* 文件名称:10.c
* 创 建 者:HSW
* 创建日期:2021年10月13日
* 描 述:
*
================================================================*/
#include <stdio.h>
/*===============================================================
* 1.取出寄存器物理地址 0xEF000000的值,确保每次都能从该地址取值,
* 该值为无符号int类型.
============================================================== */
#define MACRO_BOSE 0XEF000000
*(volatile unsigned int *)MACRO_BOSE;
/*===============================================================
* 2.用预处理指令#define 声明一个宏,用以表明1年中有多少秒(忽略闰
* 年问题, 不需要自己计算准确的秒数,使用 秒×分钟×小时×天 的格式)
============================================================== */
#define SECONDS_PER_YEAR (60 * 60 * 24 * 365)UL
/*==============================================================
* 3.嵌入式系统总是要用户对变量或寄存器进行位操作。给定一个整型变
* 量a,写两段代码,第一个设置a的bit 3,第二个清除a 的bit 3。在
* 以上两个操作中,要保持其它位不变。
==============================================================*/
#define BIT3 (0x01 << 3)
static int a = 0;
void set_bit3(void)
{
a |= BIT3;
}
void clean_bit3(void)
{
a &= ~BIT3;
}
/*===============================================================
* 4.不调用C++/C 的字符串库函数,请编写函数 strcpy已知strcpy 函
* 数的原型是 char *strcpy(char *strDest, const char *strSrc);
* 其中strDest是目的字符串strSrc 是源字符串.
* =============================================================*/
char *my_strcpy(char *strDest,const char *strSrc)
{
int i = 0;
if((NULL == strDest) || (NULL == strSrc))
{
printf("error!");
return NULL;
}
while(strSrc[i] != '\0')
{
strDest[i] = strSrc[i];
i++;
}
strDest[i] = '\0';
return strDest;
}
char *my_strcpy2(char *strDest,const char *strSrc)
{
if((NULL == strDest) || (NULL == strSrc))
{
printf("error!");
return NULL;
}
char *test = strDest;
while((*test++ = *strSrc++) != '\0');
return strDest;
}
/*==================================
*5.请使用递归算法编写求N的阶乘函数。
==================================*/
int fac(int n)
{
if(n <= 1)
{
return 1;
}
else
{
return n * fac(n-1);
}
}
/*==========================================================
* 6.写一个"标准"宏MIN ,这个宏输入两个参数并返回较小的一个。
==========================================================*/
#define MIN(a,b) ((a)<(b)?(a):(b))
/*====================================================================
*7.define一个宏,将两个byte型数合并为一个word型数,其中一个作为高8位,
*另一个作为低八位(用位操作实现)
====================================================================*/
#define WORD(HIGHT,LOW) (((HIGHT) & 0xFF)<<8 | ((LOW) & 0xFF))
/*=====================================================================
*8.实现字符查找函数char *mystrchar(char *str, char c), 查找字符串str中
*首次出现字符c的位置指针
====================================================================*/
char * mystrchar(char *str,char c)
{
while(!str)
{
if(c ==*str)
{
return str;
}
else
{
str++;
}
}
return NULL;
}
/*==========================================================
*9.写一个宏,功能:求结构体成员变量相对于其首地址的偏移地址
==========================================================*/
struct stu{
char sax;
int code;
int class;
};
#define GET_OFFSET(date,member) (&(((typeof(date)*)0)->member))
/*=================================
*10.关于float x与零值比较的if语句
=================================*/
#define LIKE_ZERO 0.000001
x==0
if((x > -LIKE_ZERO) && (x < LIKE_ZERO))
x!=0
if((x > LIKE_ZERO>) || (x < -LIKE_ZERO))
/*========================================================
*11.设地址为0x100 的整型变量的值为 2000,c语言下如何实现
========================================================*/
*((int *)0x100) = 2000;
/*============================================
*12.题目:请问运行Test 函数会有什么样的结果?
============================================*/
void GetMemory(char *p)
{
p = (char *)malloc(100);
}
void Test(void)
{
char *str = NULL;
GetMemory(str);
strcpy(str, "hello world");
printf(str);
}
/*程序崩溃。因为GetMemory 并不能传递动态内存,Test 函数中的 str 一直都是 NULL。
* strcpy(str, "hello world");将使程序崩溃。*/
char *GetMemory(void)
{
char p[] = "hello world";
return p;
}
void Test(void)
{
char *str = NULL;
str = GetMemory();
printf(str);
}
/*可能是乱码。因为GetMemory 返回的是指向“栈内存”的指针,该指针的地址不是 NULL,
* 但其原来的内容已经被清除,新内容不可知。*/
void GetMemory(char **p, int num)
{
*p = (char *)malloc(num);
}
void Test(void)
{
char *str = NULL;
GetMemory(&str, 100);
strcpy(str, "hello");
printf(str);
}
/*(1)能够输出hello(2)内存泄漏*/
void Test(void)
{
char *str = (char *) malloc(100);
strcpy(str, “hello”);
free(str);
if(str != NULL)
{
strcpy(str, “world”);
printf(str);
}
}
/*篡改动态内存区的内容,后果难以预料,非常危险。因为free(str);之后,
* str 成为野指针,if(str != NULL)语句不起作用*/
嵌入式面试题、C语言面试题、实现my_strcpy、my_strchar、标准宏等
最新推荐文章于 2024-05-12 10:42:40 发布