需要帮助 - 指针 [英] Need help - pointers
问题描述
你好,
我正在编写一个带十进制数字的程序并返回它的
二进制等价物。
问题是我不知道如何从(函数)返回
二进制值作为一个共同终止的字符数组。例如:我
想要使用4位转换数字3,即3< ----" 0011" ;.
执行后,我得到了这个警告:
警告:从不兼容的指针类型返回
行:49:警告:函数返回局部变量的地址
这是我的代码:
#include< stdio.h>
#include< stdlib.h>
#include< string.h>
int power(int base,int m);
char * dec2bin(int input,int m);
int main()
{
int input = 0;
int m = 0;
printf(" Number to convert\\\
);
scanf("%d",& input);
printf("位数\ n);
scanf("%d"& m);
printf("输入%d转换为%s \ n,输入,dec2bin(输入,m));
退出(0);
}
char * dec2bin(int input,in tm)
{
static int i = 0;
char * array [m + 1];
int limit = 0;
limit = power(2,m);
const int MASK = limit / 2; //二进制等价物是:1 + m 0位
array [m] =''\ 0'';
for(i = 0 ; i< m; i ++)
{
if(input& MASK){
strcpy(array [i]," 1");}
else {
strcpy(array [i]," 0");}
input = input< < 1;
}
返回(数组);
}
int power(int base ,int m)
{
static int i;
int p = 1;
for(i = 1; i< = m; ++ i)
{
p = p * base;
}
返回p;
}
Hi There,
I''m writing a program that takes a decimal number and returns it''s
binary equivalent.
the problem is that I don''t know how to return (from function) the
binary value as an array of cocatenated characters. For example : I
want to convert number 3 using 4 bits, ie 3 <----"0011".
After executing, I''m getting this warning:
warning: return from incompatible pointer type
line:49: warning: function returns address of local variable
Here it is my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int power (int base, int m);
char* dec2bin(int input, int m);
int main ()
{
int input = 0;
int m = 0;
printf("Number to convert\n");
scanf("%d", &input);
printf ("Number of bits\n");
scanf ("%d",&m);
printf("input %d is converted to %s\n",input,dec2bin(input, m));
exit (0);
}
char* dec2bin(int input, int m)
{
static int i = 0;
char* array[m+1];
int limit = 0;
limit = power(2, m);
const int MASK = limit/2; // the binary equivalent is: 1 + m 0 bits
array[m] = ''\0'';
for (i= 0; i < m ;i++)
{
if (input & MASK){
strcpy(array[i],"1");}
else{
strcpy(array[i],"0");}
input = input << 1;
}
return (array);
}
int power (int base, int m)
{
static int i;
int p=1;
for (i = 1; i <= m; ++i)
{
p =p * base;
}
return p;
}
推荐答案
Nezhate写道:
Nezhate wrote:
你好,
我正在编写一个带十进制数字的程序并返回它'
二进制等价物。
问题是我不知道如何从(函数)返回
二进制值作为一个共同终止的字符数组。例如:我
想要使用4位转换数字3,即3< ----" 0011" ;.
执行后,我得到了这个警告:
警告:从不兼容的指针类型返回
行:49:警告:函数返回局部变量的地址
这是我的代码:
#include< stdio.h>
#include< stdlib.h>
#include< string.h>
int power(int base,int m);
char * dec2bin(int input,int m);
int main()
{
int input = 0;
int m = 0;
printf(" Number to convert\\\
);
scanf("%d",& input);
printf("位数\ n);
scanf("%d"& m);
printf("输入%d转换为%s \ n,输入,dec2bin(输入,m));
退出(0);
}
char * dec2bin(int输入,int m)
{
static int i = 0;
char * array [m + 1];
int limit = 0;
limit = power(2,m);
const int MASK = limit / 2; //二进制等价物是:1 + m 0位
array [m] =''\ 0'';
for(i = 0 ; i< m; i ++)
{
if(input& MASK){
strcpy(array [i]," 1");}
else {
strcpy(array [i]," 0");}
input = input< < 1;
}
返回(数组);
}
int power(int base ,int m)
{
static int i;
int p = 1;
for(i = 1; i< = m; ++ i)
{
p = p * base;
}
返回p;
}
Hi There,
I''m writing a program that takes a decimal number and returns it''s
binary equivalent.
the problem is that I don''t know how to return (from function) the
binary value as an array of cocatenated characters. For example : I
want to convert number 3 using 4 bits, ie 3 <----"0011".
After executing, I''m getting this warning:
warning: return from incompatible pointer type
line:49: warning: function returns address of local variable
Here it is my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int power (int base, int m);
char* dec2bin(int input, int m);
int main ()
{
int input = 0;
int m = 0;
printf("Number to convert\n");
scanf("%d", &input);
printf ("Number of bits\n");
scanf ("%d",&m);
printf("input %d is converted to %s\n",input,dec2bin(input, m));
exit (0);
}
char* dec2bin(int input, int m)
{
static int i = 0;
char* array[m+1];
int limit = 0;
limit = power(2, m);
const int MASK = limit/2; // the binary equivalent is: 1 + m 0 bits
array[m] = ''\0'';
for (i= 0; i < m ;i++)
{
if (input & MASK){
strcpy(array[i],"1");}
else{
strcpy(array[i],"0");}
input = input << 1;
}
return (array);
}
int power (int base, int m)
{
static int i;
int p=1;
for (i = 1; i <= m; ++i)
{
p =p * base;
}
return p;
}
这是最简单的修复
只能解决由提出的一个问题你的编译器:
#include< limits.h>
char * dec2bin(int input,int m)
{
static int i = 0;
静态字符数组[sizeof input * CHAR_BIT + 1];
int limit = power (2,m);
const int MASK = limit / 2; //二进制等价物是:1 + m 0位
array [m] =''\ 0'';
for(i = 0 ; i< m; i ++)
{
if(input& MASK){
array [i] =''1' ';
}否则{
array [i] =''0'';
}
输入=输入<< 1;
}
返回(数组);
}
-
pete
This is the simplest fix
which only addresses the one issue raised by your compiler:
#include <limits.h>
char* dec2bin(int input, int m)
{
static int i = 0;
static char array[sizeof input * CHAR_BIT + 1];
int limit = power(2, m);
const int MASK = limit/2; // the binary equivalent is: 1 + m 0 bits
array[m] = ''\0'';
for (i= 0; i < m ;i++)
{
if (input & MASK){
array[i] = ''1'';
} else {
array[i] = ''0'';
}
input = input << 1;
}
return (array);
}
--
pete
Nezhate说:
Nezhate said:
你好,
我正在编写一个带小数的程序
Hi There,
I''m writing a program that takes a decimal number
没有这样的事情。数字是数字。十进制是位置符号
系统。数字可以用很多不同的方式表示。
No such thing. Numbers are numbers. Decimal is a positional notation
system. Numbers can be represented in lots of different ways.
并返回它的二进制等价物。
and returns it''s binary equivalent.
这是'单程:
#include< limits.h>
#include< stddef.h>
char * dec2bin(int n)
{
static char bin [sizeof n * CHAR_BIT + 1];
size_t b = sizeof n * CHAR_BIT;
size_t i = 0;
while( b-- 0)
{
bin [i ++] =''0''+ !!(n&(1<< b));
}
返回垃圾箱;
}
-
Richard Heathfield< http://www.cpax.org.uk>
电子邮件:-http:// www。 + rjh @
谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>
Usenet是一个奇怪的放置" - dmr 1999年7月29日
Here''s one way:
#include <limits.h>
#include <stddef.h>
char *dec2bin(int n)
{
static char bin[sizeof n * CHAR_BIT + 1];
size_t b = sizeof n * CHAR_BIT;
size_t i = 0;
while(b-- 0)
{
bin[i++] = ''0'' + !!(n & (1 << b));
}
return bin;
}
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Richard Heathfield写道:
Richard Heathfield wrote:
Nezhate说:
Nezhate said:
>嗨那里,
我正在编写一个带小数的程序
>Hi There,
I''m writing a program that takes a decimal number
没有这样的事情。数字是数字。十进制是位置符号
系统。数字可以用很多不同的方式表示。
No such thing. Numbers are numbers. Decimal is a positional notation
system. Numbers can be represented in lots of different ways.
>并返回它的二进制等价物。
>and returns it''s binary equivalent.
这里有一种方式:
#include< limits.h>
#include< stddef.h>
char * dec2bin(int n)
{
static char bin [sizeof n * CHAR_BIT + 1];
Here''s one way:
#include <limits.h>
#include <stddef.h>
char *dec2bin(int n)
{
static char bin[sizeof n * CHAR_BIT + 1];
CMIIW。如果+ 1是''\ 0''终止符,那么我们不需要那个
终结符吗?
CMIIW. If the + 1 is for the ''\0'' terminator, don''t we need that
terminator there?
size_t b = sizeof n * CHAR_BIT;
size_t i = 0;
while(b-- 0)
{
bin [i ++] =''0''+ !!(n&(1<< b));
}
>
返回bin;
}
size_t b = sizeof n * CHAR_BIT;
size_t i = 0;
while(b-- 0)
{
bin[i++] = ''0'' + !!(n & (1 << b));
}
return bin;
}
这篇关于需要帮助 - 指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!