问题写memset的功能 [英] Problems writing the memset function
问题描述
我写了 memset的
函数和我的code是下面,我有一个问题
无效* memsetFun(void *的指针,INT C中,int的大小){ 如果(指针= NULL&放大器;!&安培;尺寸大于0){ 无符号字符* PCHAR =指针;
INT I = 0; 对于(i = 0; I<大小; ++ I){ unsigned char型TEMP =(unsigned char型)C; * PCHAR ++ =温度; //或PCHAR [I] =温度(他们都没有工作) }
}
返回指针;
}
我也试过PCHAR [I] =我们想和仍然没有工作的价值。它给了我一些垃圾数量不作任何意义。
和我打电话吧:
memsetFun(地址,NUM,大小);
的printf(%2 P值为%d \\ n,地址,*((INT *)地址));
在哪里我所说的地址(我只是输入地址)
例如,如果您打印字符(三)打印像一个奇怪的字符,看起来像(为价值4)
0 0
0 4
您code看起来很好,我和这里的几个人都评论说,它工作在其系统上。
所以,很明显的事情是调试它 - 这是一个技能,会派上用场颇有几分未来:-)你应该了解它的现在的
什么以下code输出,当你运行它?
无效* memsetFun(void *的指针,INT C中,int的大小){
的printf(A%X%D \\ n,指针,C大小);
如果(指针= NULL&放大器;!&安培;尺寸大于0){
的printf(B \\ n);
无符号字符* PCHAR =指针;
INT I = 0;
对于(i = 0; I<大小; ++ I){
的printf(C%D(%D),我,* PCHAR);
unsigned char型TEMP =(unsigned char型)C;
* PCHAR ++ =温度; //或PCHAR [I] =温度(他们都没有工作)
的printf( - >(%D),我*(PCHAR-1));
}
}
的printf(D \\ n);
返回指针;
}
从输出中,应该明确的code正在采取什么样的路径,你的参数是什么(这将大大有助于调试过程)。
的更新:的
如果你与1比0以外的任何尽显你的内存块,并使用这样的:
的printf(%2 P值为%d \\ n,地址,*((INT *)地址));
要打印出来,你的将会的得到奇怪的结果。
你基本上询问了一些这些字节是PTED为整数间$ P $。因此,举例来说,如果你用 0×02
字节填充它,你有一个4字节的整数类型,你会得到整数 0x02020202
(33686018)的不的 0×02
,你可能期望。如果你想看看第一的字符的值,使用:
的printf(%2 P值为%d \\ n,地址,*((字符*)地址));
和基于您的最新的的问题更新:
例如,如果您打印字符(三)打印像一个奇怪的字符,看起来像(该值4)结果
0 0结果
0 4
块引用>如果这是一个单一的性格和你打印它作为一个字符,则可能是没有错的。许多输出流会给你,对于一个控制字符(CTRL-D在这种情况下,ASCII code 4)。如果你不是用ASCII code的0x30(48),你会看到的字符'0'或ASCII×41(65)填充它会给你'A'。
I am writing the
memset
function and my code is below, I am having a problemvoid* memsetFun(void* pointer, int c, int size) { if ( pointer != NULL && size > 0 ) { unsigned char* pChar = pointer; int i = 0; for ( i = 0; i < size; ++i) { unsigned char temp = (unsigned char) c; *pChar++ = temp; // or pChar[i] = temp (they both don't work) } } return pointer; }
I also tried pChar[i] = the value we want and still not working. It gives me some trash numbers that do not make any sense.
And I am calling it:
memsetFun(address, num, size); printf("value at %p is %d\n", address, *((int*) address));
Where I call the address (I just input the address)
For example, if you to print the chars ( c ) it prints like a weird char that looks like ( for the value 4 )
0 0 0 4
解决方案Your code looks fine to me and several people here have commented that it works on their system.
So the obvious thing to do is to debug it - that's a skill that will come in handy quite a bit in future :-) You should learn it now.
What does the following code output when you run it?
void* memsetFun(void* pointer, int c, int size) { printf("A %x %d %d\n", pointer, c, size); if ( pointer != NULL && size > 0 ) { printf("B\n"); unsigned char* pChar = pointer; int i = 0; for ( i = 0; i < size; ++i) { printf("C %d (%d)", i, *pChar); unsigned char temp = (unsigned char) c; *pChar++ = temp; // or pChar[i] = temp (they both don't work) printf(" -> (%d)", i, *(pChar-1)); } } printf("D\n"); return pointer; }
From the output, it should be clear what paths the code is taking and what your parameters are (which will greatly assist the debugging process).
Update:
If you're filling your memory block with anything other than zeros and using this:
printf("value at %p is %d\n", address, *((int*) address));
to print it out, you will get strange results.
You're basically asking for a number of those bytes to be interpreted as an integer. So, for example, if you filled it with
0x02
bytes and you have a 4-byte integer type, you will get the integer0x02020202
(33686018), not0x02
as you may expect. If you want to see what the first character value is, use:printf("value at %p is %d\n", address, *((char*) address));
And based on your latest question update:
For example, if you to print the chars ( c ) it prints like a weird char that looks like ( for the value 4 )
0 0
0 4If that's a single character and you're printing it as a character, there's probably nothing wrong at all. Many output streams will give you that for a control character (CTRL-D in this case, ASCII code 4). If you instead filled it with ASCII code 0x30 (48), you would see the character '0' or ASCII 0x41 (65) would give you 'A'.
这篇关于问题写memset的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!