char a[];
char *b;...
b = (char *) malloc(100);
a = b;
如果不行,如何转化?

解决方案 »

  1.   

    例:char a[1],此时a相当于是一个const char*,是不能再赋值的。
      

  2.   

    char* a;
    char* b;b = (char *)malloc(100);a = b; // OK
    char a[];
    a = (char *)malloc(100); //Error!char* b;
    b = (char *)malloc(100); // OKa = (char *)0x100000; //Error
    b = (char *)0x100000; //OK理由:
    char a[];说明a是数组,数组的第一个元素的地址是静态分配的,不能再赋值
    char *b;说明b是指针,指针可以在运行时被赋值,来指向不同的内存地址
      

  3.   

    让我们来看一下编译器是如何处理数组的:
    7:        int a[10];
    8:        a[0]=1;
    00401028   mov         dword ptr [ebp-28h],1编译器直接把a[0]变成地址,也就是说不存在一个变量保存数组的首地址。编译器已经分配好了地址,所以就不能改变了。
      

  4.   

    续楼上兄弟的,当年我写过8086的汇编编译程序,对于这两种指针,我的汇编是这么写的
    a: db '          '
    b: dw 0
    ....
    2848:0200 20
    2848:0200 20
    ...
    ;这上面就是a的一个实现,a指向2848:0200
    2848:020a 00
    2848:020a 00
    ;b指针占据2848:020a,但是b的值为0,可以让b指向a,汇编的方法就是
    mov [b],a
    mov [020a],0200
    //b = ( char * )a;
    以上是个人意见。
      

  5.   

    是这样的有一个结构
    struct msgbuf{
     long mtype;
     char mtext[];
    }
    int msgsnd(int msgid,struct msgbuf *buf,...);
    我自己定义了
    struct mymsg{
     long mtype;
     char *pbuf;
    }buf;
    msgsnd(msgid,(struct msgbuf *)buf,....);
    是可以的
      

  6.   

    不行错误  声明字符串是系统会首先为期分配空间,即a[i],i不能为空,切是定值
          与此同时a相当于一指向一定给字符数组织指针常量       
    你可以将a的值付给b,但不可将b的值付给a
      

  7.   

    你想做什么用,是不是想在
    程序运行时确定char 的大小。
    #include<iostream.h>
    void main()
    {
      int iArray;
      char *a;
      cout<<"please inupt number a"<<endl;
      cin>>iArray;
      if((a=new char[iArray])==NULL)
        {
           cout<<"can't malloc more memory"<<endl;
           exit(1);
         }
    ……//加入你要给a 的植即可。}