c语言 可变数组

来源:互联网 发布:免费空间支持域名绑定 编辑:程序博客网 时间:2024/06/05 06:14
#include <stdio.h>#define bzero(b,len) (memset((b), '/0', (len)), (void) 0)//在支持的c99 的编译器下运行如gcc  vc不支持c99标准中的c语言变长数组int main(int argc, char *argv[]){int i, n;if(argc!=2){printf("usge:%s int;",argv[0]);return -1;}n = atoi(argv[1]);char arr[n+1];bzero(arr, (n+1) * sizeof(char));for (i = 0; i < n; i++) {arr[i] = (char)('A' + i);}arr[n] = '\0';printf("%s\n ok", arr);//getchar();return (0);}
#include <iostream>using namespace std;struct MyData{int nLen;char data[0];} mydata;//C语言变长数组 struct中char data[0]的用法//http://www.2cto.com/kf/201312/261179.htmlint main(){//int nLen = 10;char str[] = "123456789fdasfaadfsdfaf";cout << "Size of MyData: " << sizeof(MyData) << endl;cout << "Size of str: " << strlen(str) << endl;MyData *myData = (MyData*)malloc(sizeof(MyData) + strlen(str));memcpy(myData->data, str, strlen(str));cout << "myData's Data is: " << myData->data << endl;free(myData);int n;cin>>n;return 0;}

//变长一维数组#include<iostream>   using namespace std;   int   main()   {   int len; cout<<"please input the len;"<<endl;cin>>len;   //用指针p指向new动态分配的长度为len*sizeof(int)的内存空间   int  *p=new int[len];  for (int i=0;i<len;i++){p[i]=i+100;}for(int j=0;j<len;j++){cout<<p[j]<<" ";}delete[]  p;  int n;cin>>n;return 0;   }   
//使用C++标准模版库(STL)中的vector实现变长数组#include<iostream> #include <vector>using namespace std;   int   main()   {   int len;   cin>>len;   vector<int>  arr(len);//声明变长数组for(int   i=0;i<len;i++)   {   arr[i]=i;   cout<<arr[i]<<"\t";   }arr.clear();arr.empty();int n;cin>>n;return 0;   }   

//c语言实现变长n维数组  #include <stdio.h>       #include <malloc.h>       void main()       {       int     x,y,i,j;       float     **a,*b;       printf("请输入你所求解的线性方程组的行数x:x=");       scanf("%d",&x);       printf("请输入你所求解的线性方程组的列数y:y=");       scanf("%d",&y);       a=(float**)malloc(sizeof(float*)*x);       b=(float *)malloc(sizeof(float)*x);       for(i=0;i<x;i++)       {       *(a+i)=(float*)malloc(sizeof(float)*y);       }       printf("请按行的顺序依次输入系数的值(共%d项):",x*y);       for(i=0;i<=x-1;i++)       for(j=0;j<=y-1;j++)       scanf("%f",&a[i][j]);       printf("请按列的顺序依次输入常数的值(共%d项):",x);       for(j=0;j<=x-1;j++)       scanf("%f",&b[j]);       printf("您输入方程组的增广矩阵为:\n");       for(i=0;i<=x-1;i++)       {       for(j=0;j<=y-1;j++)       printf("%.5f         ",a[i][j]);       printf("%.5f         ",b[i]);       printf("\n");       }       free(b);       for(i=0;i<x;i++)       free(*(a+i));}   

#include   <iostream>   #include   <iomanip>   using   namespace   std;  //c++ 变长n维数组  int   main()   {   int   num1;//行数   intnum2;//列数   cout<<"Please   enter   the   number   for   row   and   column:   "<<endl;   cin>>num1>>num2;   //为二维数组开辟空间   int   **p   =   new   int*[num1];   for(int   i=0;   i<num1;   ++i)   p[i]   =   new   int[num2];   for(int   j=0;j<num1;j++)   {   for(int   k=0;k<num2;k++)   {   p[j][k]=(j+1)*(k+1);   cout<<setw(6)<<p[j][k]<<':'<<setw(8)<<&p[j][k];   }   cout<<endl;   }   //释放二维数组占用的空间   for(int   m=0;m<num1;m++)   delete[]   p[m];   delete[]   p;   int m;cin>>m;return   0;   }   

#include   <iostream>   #include   <vector>   #include   <iomanip> using namespace std;  //vector(向量)实现二维数组呢int   main()   {   int   i,j, m, n; cout <<"input   value   for   m,n:";   cin>>m>>n;   //注意下面这一行:vector<int后两个">"之间要有空格!否则会被认为是重载">>"。   vector<vector<int>   >  vecInt(m,   vector<int>(n));       for(i=0;i <m;i++)   for(j=0;j<n;j++)   vecInt[i][j]=i*j;     for(i=0;i<m;i++)   {   for(j=0;j<n;j++)   cout<<setw(5)<<vecInt[i][j]<<":"<<setw(9)<<&vecInt[i][j];   cout<<endl;   }  int t;cin>>t;return 0;   }   
#include <stdio.h>#include <string.h>#include <stdlib.h>//妙用0元素数组 实现大小可变结构体struct aa{ int a;int b;};struct bb{ struct aa test[0];};int main(void){struct bb *p=(struct bb*)malloc(sizeof(struct bb)+sizeof(struct aa)*100);p->test[0].a=10;p->test[0].b=20;printf("%d,%d\n",p->test[0].a,p->test[0].b);system("pause");return 0;}

看这个结构体的定义:
typedef struct st_type
{
     int nCnt;
     int item[0];
}type_a;
(有些编译器会报错无法编译可以改成:)
typedef struct st_type
{
     int nCnt;
     int item[];
}type_a;
    这样我们就可以定义一个可变长的结构,用sizeof(type_a)得到的只有4,就是sizeof(nCnt)=sizeof(int)那个0个元素的数组没有占用空间,而后我们可以进行变长操作了。
        C语言版:        type_a *p = (type_a*)malloc(sizeof(type_a)+100*sizeof(int));
        C++语言版:        type_a *p = (type_a*)new char[sizeof(type_a)+100*sizeof(int)];
    这样我们就产生了一个长为100的type_a类型的东西用p->item[n]就能简单地访问可变长元素,原理十分简单,分配了比sizeof(type_a)多的内存后int item[];就有了其意义了,它指向的是int nCnt;后面的内容,是没有内存需要的,而在分配时多分配的内存就可以由其来操控,是个十分好用的技巧。
而释放同样简单:
        C语言版:free(p);
        C++语言版:delete []p;
    这个被称为灵活/弹性数组成员(fleible array member)C89不支持这种东西,C99把它作为一种特例加入了标准。但是,C99所支持的是incomplete type,而不是zero array,形同int item[0];这种形式是非法的,C99支持的形式是形同int item[];只不过有些编译器把int item[0];作为非标准扩展来支持,而且在C99发布之前已经有了这种非标准扩展了,C99发布之后,有些编译器把两者合而为一。


    下面是C99中的相关内容:
6.7.2.1 Structure and union specifiers
    As a special case, the last element of a structure with more than one named member may have an incomplete array type; this is called a flexible array member. With two exceptions, the flexible array member is ignored. First, the size of the structure shall be equal to the offset of the last element of an otherwise identical structure that replaces the flexible array member with an array of unspecified length.106) Second, when a . (or ->) operator has a left operand that is (a pointer to) a structure with a flexible array member and the right operand names that member, it behaves as if that member were replaced with the longest array (with the same element type) that would not make the structure larger than the object being accessed; the offset of the array shall remain that of the flexible array member, even if this would differ from that of the replacement array. If this array would have no elements, it behaves as if it had one element but the behavior is undefined if any attempt is made to access that element or to generate a pointer one past it.

注意区分 C99新增的“可变长数组”:
C89 标准规定,数组大小必须是在编译时刻确定的;在C99 中,这个标准项被扩展,可以是运行时刻确定的值。也就是说, 可变长数组和 C++ 本身没有关系,只要是支持 C99 的就可以使用可变长数组,包括支持 C99 的 C 编译器。

需要注意的是,可变长数组的维数在数组生存期内是不变的,也就是说,可变长数组不是动态的,可变的只是数组的大小。

引进这一特性的目的是为了支持数值处理。



1 0