A. c語言創建數組的3種方法是什麼,求例子
第一個,
int a[10];
這里的10就是常量。
第二個 用變數
int n=10;
int a[n];
很多編譯器會不支持。
第三個
int *a;
a=(int *)malloc(sizeof(int)*10);
使用後,要對a做free(a);
B. js中json數組問題,怎麼創建json數組
普通的數組格式是:['a','b','c']
JSON的格式是:{'1':'a','2':'b','3':'c'}
所以把數組循環一下就可以了;
var a = ['a','b','c'];var json = {};for(var i=0;i<a.length;i++){ json[i]=a[i];}JSON.stringify(json); //結果:{'1':'a','2':'b','3':'c'}
C. 怎麼把把 json 轉換成數組
有兩種方法實現:
(1)直接轉換:
創建JSONArray對象、JSONObject對象,通過put方法將其數據以鍵值對的形式填充,如例:
//假如有一個Java實體類Person,包括以下屬性:phone、sex、married、address。
ArrayList<Person> persons = new ArrayList<Person>();
for (int i = 0; i < 3; i++) {
persons.add(new Person("138******67", "女", true, "北京朝陽區"));//填充Java實體類集合
}
JSONArray array = new JSONArray();// Json格式的數組形式
JSONObject obj;//json格式的單個對象形式
for (int i = 0; i < persons.size(); i++) {
obj = new JSONObject();
//依次填充每一個Java實體對象
try {
obj.put("phone", persons.get(i).getPhone());//json通過put方式以key-value形式填充
obj.put("sex", persons.get(i).getSex());
obj.put("married", persons.get(i).getMarried());
obj.put("address", persons.get(i).getAddress());
array.put(obj);//將JSONObject添加入JSONArray
} catch (JSONException e) {
e.printStackTrace();
}
}
(2)通過GSON工具實現:
只需要創建GSON對象,通過toJson方法轉換即可。如例:
//模擬Java數據
Person person = new Person("敏敏", 23, "女", "北京市海淀區");
//創建GSON對象
Gson gson = new Gson();
//調用toJson方法即可轉換為字元串的json格式
String json = gson.toJson(person);
D. c語言或c++如何自由創建數組
%其實C++是不容許用變數來定義數組的,例如:int m;cin>>m; int a[m];這樣是不行的。但是我們可以換一種思路。你看看這樣,我們可以先申請的變數a,再用new運算符來建立臨時數組。這樣問題就解決了。用這個方法來解決上面你提到的兩個問題(假設你向a中輸入5),看代碼:第一個問題, int a; cin>>a; int *p=new int[a]; 第二個問題,int a;cin>>a;int *p=new int[a*(a-1)]; 這樣就可通過指針p來訪問數組中的值了。%
E. C語言新建一個數組的語法
數據類型
數組名[維數]
如:int
a[5];定義了一個長度為5的一維數據
F. c語言結構數組,如何創建外部數組和靜態數組
外部變數就是引用外部文件的變數(非靜態)
靜態變數與全局變數相似,區別是靜態變數只能在定義的函數內被調用。
靜態變數與局部變數區別:靜態變數不會自動釋放,函數調用結束後,值依然保留。靜態變數定義時賦初值,只有一次,多次調用函數,不會反復賦初值。如果沒有初值,靜態變數也會自動賦初值。
關於數組定義在棧中維度大小受限,想要定義在堆中,可以用malloc申請。
詳細看我寫的案例備注吧。
//externFile.c文件
#include<stdio.h>
structbook2
{
intid;
};
structbook3
{
intid;
};
structbook2bk2;
staticstructbook3bk3;//b3是靜態全局變數,無法通過extern被外部調用
G. C語言如何創建數組
動態數組:void creat(linklist &L)
{
int x;
L=new Lnode;
linklist p,u;
p=L;
cout<<"請輸入一些有序的整數,以負數結束:"<<endl;
cout<<"請輸入一個整數:"<<endl;
cin>>x;
while(x>0)
{u=new Lnode;
u->data=x;
p->next=u;
p=p->next;
cin>>x;
}
p->next=NULL;
} 靜態數組:int iArray[10]={1,1,2,3,5,8,13,21,34,55); //初始化
void main()
{
//...
}
H. 怎麼用C語言獲取JSON中的數據
用C語言獲取JSON中的數據的方法是使用 CJSON。
以下簡單介紹用CJSON的思路及實現:
1)創建json,從json中獲取數據。
#nclude <stdio.h>
#include "cJSON.h"
char * makeJson()
{
cJSON * pJsonRoot = NULL;
pJsonRoot = cJSON_CreateObject();
if(NULL == pJsonRoot)
{
//error happend here
return NULL;
}
cJSON_AddStringToObject(pJsonRoot, "hello", "hello world");
cJSON_AddNumberToObject(pJsonRoot, "number", 10010);
cJSON_AddBoolToObject(pJsonRoot, "bool", 1);
cJSON * pSubJson = NULL;
pSubJson = cJSON_CreateObject();
if(NULL == pSubJson)
{
// create object faild, exit
cJSON_Delete(pJsonRoot);
return NULL;
}
cJSON_AddStringToObject(pSubJson, "subjsonobj", "a sub json string");
cJSON_AddItemToObject(pJsonRoot, "subobj", pSubJson);
char * p = cJSON_Print(pJsonRoot);
// else use :
// char * p = cJSON_PrintUnformatted(pJsonRoot);
if(NULL == p)
{
//convert json list to string faild, exit
//because sub json pSubJson han been add to pJsonRoot, so just delete pJsonRoot, if you also delete pSubJson, it will coremp, and error is : double free
cJSON_Delete(pJsonRoot);
return NULL;
}
//free(p);
cJSON_Delete(pJsonRoot);
return p;
}
void parseJson(char * pMsg)
{
if(NULL == pMsg)
{
return;
}
cJSON * pJson = cJSON_Parse(pMsg);
if(NULL == pJson)
{
// parse faild, return
return ;
}
// get string from json
cJSON * pSub = cJSON_GetObjectItem(pJson, "hello");
if(NULL == pSub)
{
//get object named "hello" faild
}
printf("obj_1 : %s
", pSub->valuestring);
// get number from json
pSub = cJSON_GetObjectItem(pJson, "number");
if(NULL == pSub)
{
//get number from json faild
}
printf("obj_2 : %d
", pSub->valueint);
// get bool from json
pSub = cJSON_GetObjectItem(pJson, "bool");
if(NULL == pSub)
{
// get bool from json faild
}
printf("obj_3 : %d
", pSub->valueint);
// get sub object
pSub = cJSON_GetObjectItem(pJson, "subobj");
if(NULL == pSub)
{
// get sub object faild
}
cJSON * pSubSub = cJSON_GetObjectItem(pSub, "subjsonobj");
if(NULL == pSubSub)
{
// get object from subject object faild
}
printf("sub_obj_1 : %s
", pSubSub->valuestring);
cJSON_Delete(pJson);
}
int main()
{
char * p = makeJson();
if(NULL == p)
{
return 0;
}
printf("%s ", p);
parseJson(p);
free(p);//這里不要忘記釋放內存,cJSON_Print()函數或者cJSON_PrintUnformatted()產生的內存,使用free(char *)進行釋放
return 0;
}
2)創建json數組和解析json數組
//創建數組,數組值是另一個JSON的item,這里使用數字作為演示
char * makeArray(int iSize)
{
cJSON * root = cJSON_CreateArray();
if(NULL == root)
{
printf("create json array faild ");
return NULL;
}
int i = 0;
for(i = 0; i < iSize; i++)
{
cJSON_AddNumberToObject(root, "hehe", i);
}
char * out = cJSON_Print(root);
cJSON_Delete(root);
return out;
}
//解析剛剛的CJSON數組
void parseArray(char * pJson)
{
if(NULL == pJson)
{
return ;
}
cJSON * root = NULL;
if((root = cJSON_Parse(pJson)) == NULL)
{
return ;
}
int iSize = cJSON_GetArraySize(root);
for(int iCnt = 0; iCnt < iSize; iCnt++)
{
cJSON * pSub = cJSON_GetArrayItem(root, iCnt);
if(NULL == pSub)
{
continue;
}
int iValue = pSub->valueint;
printf("value[%2d] : [%d] ", iCnt, iValue);
}
cJSON_Delete(root);
return;
}
I. 關於C語言數組的創建
兩種方法:
轉化為一維數組申請
先申請全部行首指針,再按行逐行申請
1、方法一:
a=(int*)malloc(sizeof(int),(unsigned)m*n);
使用的時候就和一般的二維數組一樣。
舉個例子給你:
#include"stdlib.h"
#include"stdio.h"
#include<malloc.h>
intmain()
{
inti,j;
intn;//這個就是需要指定的行數
int(*p)[10];
scanf("%d",&n);//取得行數
//動態生成二維數組,指定列數為10,如果想改,自己該裡面
的參數,如果想定義n行2列就為:p=(int(*)[2])malloc(n*2*sizeof(int));
p=(int(*)[10])malloc(n*10*sizeof(int));//動態申請n行10列的二維數組
for(i=0;i<n;i++)
for(j=0;j<10;j++)
p[i][j]=i*j;
for(i=0;i<n;i++)
{
for(j=0;j<10;j++)
printf("%d,",p[i][j]);
printf(" ");
}
free(p);
return0;
}
2、方法二:
#include<stdio.h>
#include<malloc.h>
main()
{
inti=0;
intj=0;
intline=0;
introw=0;
int**p=NULL;
printf("inputthelineofthearray: ");
scanf("%d",&line);
printf("inputtherowofthearray: ");
scanf("%d",&row);
p=(int**)malloc(sizeof(int*)*line);
if(NULL==p)
{
return;
}
for(i=0;i<line;i++)
{
*(p+i)=(int*)malloc(sizeof(int)*row);
if(NULL==*(p+i))
{
return;
}
}
/*inputdata*/
for(i=0;i<line;i++)
{
for(j=0;j<row;j++)
{
p[i][j]=i+1;
}
}
/*outputdata*/
for(i=0;i<line;i++)
{
for(j=0;j<row;j++)
{
printf("%d",p[i][j]);
}
}
/*freeeverylinepoint*/
for(i=0;i<line;i++)
{
free(*(p+i));
p[i]=NULL;
}
free(p);
p=NULL;
}
J. C語言創建一個數組
定義一個指針,將數據的第一個賦給頭指針,例如:int *p;*p=a;p++;*p=b;以此類推,等等: