Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

easy qestion, get a free star ;0) SegFault+Printf+char * 1

Status
Not open for further replies.

LinuXelite

Programmer
Jun 21, 2002
150
CA
Hi!! Im a beginner... look at this:

int main(int argc, char *argv[])
{
char *a;
char *b;

strcpy(a,"abc");
strcpy(b,"def");

strcat(a,b);
printf("%s\n",a)
}


gcc test.c - o test
./test
Segmentation fault.

Why??

Secondo:

void Analiser_Buffer(gchar *buffer) {
guint i;
char *ss;

strcpy(ss,"");
for(i = 0; i < strlen(buffer) - 1; i++) {
if ((buffer == 13) && (buffer[i+1]) == 10){
Analiser_Ligne(ss);
strcpy(ss, &quot;&quot;);
} else {
strcat(ss, buffer); // WARNING,<--- dont work!
printf(&quot;%s&quot;,buffer); // SEGFAULT;;; WHY?
printf(&quot;%c&quot;,buffer); // WORKS!
}
}


Thank you!!

Frank,




 
You never allocate memory for your char* variables.
Use &quot;malloc&quot; to dynamically allocate memory. When you're done, use &quot;free&quot; to free it.
 
The two lines that read...
Code:
   char *a;
   char *b;
...don't allocate any memory. The '*' means that these are just pointers to memory. You need to allocate some memory to actually hold the stuff you're manipulating. So, [tt]malloc()[/tt] is the call to allocate memory, and [tt]free()[/tt] is the call to free it up. This would make your [tt]main()[/tt] function look like...
Code:
int main(int argc, char *argv[]) 
{   
  char *a = malloc(1000); /* or whatever size you need */
  char *b = malloc(1000);

  strcpy(a,&quot;abc&quot;);
  strcpy(b,&quot;def&quot;);

  strcat(a,b);
  printf(&quot;%s\n&quot;,a)

  free(a);
  free(b);
}
Actually, since the [tt]main()[/tt] function is ending right then, you really don't need to free the memory because it will be freed when the program exits. It is a good habit to free any memory you allocate though.

Your other example just needs the same kind of thing. The pointer [tt]ss[/tt] needs to point to something. It's uninitialized. Either malloc some memory for it to point to or point it to an existing character array variable.

Hope this helps.

 
Ok I know MAN () :)

Now what am I doing wrong here?

void Analiser_Buffer(char buffer[85]) {
guint i;
char ss[85];

strcpy(ss,&quot;&quot;);

for(i = 0; i < strlen(buffer) - 1; i++) {
if ((buffer == 13) && (buffer[i+1]) == 10){
Analiser_Ligne(ss);
strcpy(ss, &quot;&quot;);
} else {
strcat(ss, buffer); // ERROR ERROR ERROR********
}
}
}

strcat cannot add buffer to ss?


 
This is a little off topic, but the reason your code listing goes italic half way through is because you are using an &quot;i&quot; for an array index. The string &quot;
Code:
[i]
&quot; is a TGML tag for putting it into italics. If you click the link below the &quot;Your Reply&quot; box that says &quot;Process TGML&quot;, it will show you the different tags you can put in your post.

If you put the tags [tt][ignore]
Code:
[/ignore][/tt] and [tt][ignore]
[/ignore][/tt] before and after your code, it won't do that and your
Code:
buffer[i]
won't get messed up.

Hope this helps.

 
Thank you...

But what is the problem with this line?

void Analiser_Buffer(char buffer[1200]) {
guint i;
char ss[85];

strcpy(ss,&quot;&quot;);
printf(&quot;*** Nouveau buffer ***\n&quot;);
for(i = 0; i < strlen(buffer) - 1; i++) {
if ((buffer == 13) && (buffer[i+1]) == 10){
Analiser_Ligne(ss);
strcpy(ss, &quot;&quot;);
} else {
strcat(ss, buffer); // ERROR - SEGFAULT
}
}
}
 
I'm not completely sure what you're trying to do, but try this...
Code:
#include <stdlib.h>

void Analiser_Buffer(char * buffer) {
  int  i, j;
  char * ss = malloc(strlen(buffer)+10); /* enough plus padding */

  strcpy(ss,&quot;&quot;); j = 0; /* reset ss */

  for(i = 0; i < strlen(buffer); i++) {
    if ((buffer[i] == (char) 13) && (buffer[i+1]) == (char) 10){
      Analiser_Ligne(ss);    /* output(?) ss */
      strcpy(ss, &quot;&quot;); j = 0; /* reset ss */
      ++i;   /* need to skip the LF char (10) */
    } else {
      *(ss + j++) = buffer[i]; /* grab just this character */
      *(ss + j) = (char) NULL; /* keep ss NULL terminated */
    }
  }

  free(ss);
}
This should work (I haven't tested it). You would use it like...
Code:
   char somearray[2000];

   /* some code to put something into somearray goes here */

   Analiser_Buffer(&somearray);
Again, I haven't tested this, but this is basically how it would be put together.

Hope this helps.

 
I would guess it's because you are giving [tt]strcat[/tt] a second argument of a [tt]char[/tt], which it then treats as a pointer which would cause the segmentation fault.
[tt]strcat[/tt] appends one string to another, not a character.

//Daniel
 
Or you could change the line...
Code:
strcat(ss, buffer[i]); // ERROR - SEGFAULT
...to...
Code:
strcat(ss, &buffer[i]);
I think! If you look at the function prototype for [tt]strcat()[/tt], it's two parameters are pointers to characters. The
Code:
buffer[i]
is not a pointer, but the character at that offset into the array [tt]buffer[/tt]. Since the &quot;character&quot; at that place in the buffer is not a valid memory address, it's out of your process' memory space and generates a segmentation fault. In other words, your second parameter that you are sending the [tt]strcat()[/tt] is a &quot;j&quot; or an &quot;R&quot; or whatever character is at that point in the buffer, and NOT a valid address. I'm not sure if I explained that clearly, but, oh well.

Also, keep in mind that [tt]strcat()[/tt] will concatenate a whole NULL terminated string onto another NULL terminated string. Since you are stepping through [tt]buffer[/tt] one character at a time, I don't think [tt]strcat()[/tt] is what you want at this point. That's why my example just copies the single character, then NULL terminates [tt]ss[/tt].

Hope this helps.

 

Sometimes what you need is not a (MAN)UAL but the advice of a pro. SamBones, that's exactly what you gave me.

I dont know how to thank you I finally understood something.
You code works perfectly.

Im going to work on that and try to get more experience.

Im programming a GNU software that grab, analyse, send to mysql and create graph from a PBX. Its really hard for a php, perl programmer to use ANSI C.

Thank a lot again... I hope I'll be able to advance my projet now.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top