我正在嘗試使用calloc爲結構變量分配內存,但出現分段錯誤。當我嘗試使用ddd進行調試時,在將第一個哈希名分配給代碼是結構變量的成員。使用calloc將內存分配給Struct變量時出現分段錯誤
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
static char vcd_xyz[5];
static char vcd_xyz1[2];
char getvariablename();
void printmyvalue(char a[]);
void passhashnamevalue(char a[]);
typedef struct Variable_struct
{
char *name;
char *hashname;
}Variable;
typedef struct Newstruct
{
Variable *Variables;
}sss;
int main()
{
getvariablename();
}
char getvariablename()
{
int i,j;
vcd_xyz[4] = '\0';
int count = 0;
for(i=0;i<26;i++)
{
vcd_xyz[0] = 'a'+i;
// printf("%d generated variable is initial is = %c \n",i,vcd_xyz[0]);
for(j=0;j<26;j++)
{
vcd_xyz[1] = 'a'+j;
// printf("%d generated variable is = %c \n",j,vcd_xyz[1]);
// puts(vcd_xyz);
for(int k = 0;k<26;k++)
{
vcd_xyz[2] = 'a' + k;
// puts(vcd_xyz);
for(int l=0;l<26;l++)
{
vcd_xyz[3] = 'a' +l;
count ++;
passhashnamevalue(vcd_xyz);
//printmyvalue(vcd_xyz);
// printf("%s\n",vcd_xyz);
}
}
}
}
return vcd_xyz[4];
}
void printmyvalue(char a[])
{
printf("%s \n",a);
}
void passhashnamevalue(char a[])
{ sss *SSS;
SSS->Variables = (Variable *) calloc(15,sizeof(Variable));
for(int i=0;i<=10;i++)
{
SSS->Variables[i].hashname = (char*)calloc(strlen((char*)a)+1,sizeof(char));
strcpy(SSS->Variables[i].hashname,(char*)a);
printf("%s",SSS->Variables[i].hashname);
}
}
我無法揣摩出我做mistake.This代碼看起來好像有點搞砸了,但其在延續my previous question
林不是一個挑剔,但「passhashnamevalue」會看起來好多了,因爲「PassHashNameValue」 – TsSkTo