I have this array:
char *tags[100];
If I do this:
tags[0]="something";
It works (no errors at least). However, the very same code in a for loop doesn’t.
int j=0;
for(j; j<100; ++j)
{
tags[j]="something";
}
It says “Segmentation fault”. What is this?
UPDATE: Whole code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void append(char* s, char c)
{
int len = strlen(s);
s[len] = c;
s[len+1] = '\0';
}
int main()
{
int istag;
FILE *fopen(), *fp;
int i;
fp = fopen("oldal.html","r");
i= getc(fp) ;
char* szo;
int index=0;
char *tags[100];
int j=0;
for(j; j<100; ++j)
{
tags[j]="something";
}
while (i!= EOF)
{
i = getc(fp);
char c=i;
if(c=='<')
{
istag=1;
}
if(c=='>')
{
istag=0;
index++;
//printf("tag vege: %s %d",tags[index],index);
}
if(istag)
{
//append(tags[index],'a');
}
append(szo,c);
}
//printf("%s",szo);
fclose(fp);
return 0;
}
Your code calls your append() function to append data to
szo, butszowas never initialized and contains whatever value happened to be on the stack where it is declared.Who knows where it is writing to. This is almost certainly the cause of the problem.
Initialize
szoby pointing it to a character buffer of sufficient size. Remember to make it big enough to hold even the data and terminators that are appended.