问题描述
我的目的是创建函数void print_chars(char *imp)
来打印字符串的元素。使用指针通过引用将字符串传递给函数。
#include <stdio.h>
#define len(x,y) sizeof(x) / sizeof(y)
void print_chars(char*);
/* Function to print the characters of a string passed by reference to
* the function.*/
int main(int argc,char *argv[])
{
/* Definition of the string and its characteristics. */
/* The "lvalue" of the string is "m_str". */
char m_str[] = "123456\n89 ab";
int length_a = len(m_str,m_str[0]);
/* Let's point to the string to print it*/
char *imprimatur = m_str;
print_chars(imprimatur);
return 0;
}
void print_chars(char *imp)
{
unsigned int i = 1;
char *c;
for (c = imp; *c != '\0'; c++,i++)
printf("xxx[%d] = %c\n",i,*c);
/* ^ How to write here the original name of the lvalue
* originally assigned outside the funcion? */
}
这是输出示例:
xxx [1] = 1
xxx [2] = 2
...
这是预期的输出:
m_str [1] = 1
m_str [2] = 2
...
C中是否有任何函数可以让我获得先前声明的变量的特征,例如其左值?
谢谢!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)