1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Segfault when using Pointer to pointer

Discussion in 'Meet and Greet' started by shree249, Jul 20, 2012.

  1. shree249

    shree249 New Member

    Hi, I am writing a c++ code which uses a pointer to pointer of type char*. I get a segfault while executing it.
    When i replace the pointer to pointer variable with array of pointer variable it works. i.e when i use char **var i get a segfault, but when i use char *var[51] the code works as expected. as per my limited knowledge char **var and char *var[51] mean the same thing only the difference being the former does not have a limit.

    The code is given below. This code basically scans a file named filename.txt and matches a line and stores the address of the IP string in pointer to pointer.
    The contents of the file is
    user@servername:~$ cat filename.txt
    order deny,allow
    deny from all
    allow from
    #include <arpa/inet.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    int main(int argc, char *argv[])
            char *requestip;
            requestip = "";
            printf("%s\n", requestip);
            char *ipstring,*token;
            [B]char **ips;[/B]
            int j,count;
            int foundline;
            char tmp_line[1024];
            FILE *fp = fopen("filename.txt", "r");
            if(fp == NULL)      return 1;
            while(fgets(tmp_line,1023,fp)!=0)      {
                    if(strcasestr(tmp_line,"order deny,allow"))   {       foundline=1;   continue;       }
                    if(foundline==1)                              {       if(strcasestr(tmp_line,"deny from all")==NULL)  return 0;       foundline++; continue;}
                    if(foundline==2)        {
                            ipstring = strcasestr(tmp_line,"allow from");
                            if(ipstring != NULL)    {
                                    for (j=0;;j++,ipstring=NULL)    {
                                            token = strtok(ipstring," ");
                                            if (token == NULL) break;
                                            [B]ips[j] = token;[/B]
                            else    return 0;
                    if(!feof(fp)) break;
    Please let me know if i am doing anything wrong.
    gcc version 4.5.2 (GCC)
    OS: slackware 13
    Last edited by a moderator: Jul 20, 2012

Share This Page