help on double pointer

This is a discussion on help on double pointer within the C Programming forums, part of the General Programming Boards category; I was using double pointer ( I am not expert programmer) in a function of my program to read some ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    19

    help on double pointer

    I was using double pointer ( I am not expert programmer) in a function of my program to read some strings (with variable length) and I am getting error (run time error not compilation error). I just wrote a test program to understand this. In this program, if in function test1, v3+3 and v3+4 are also initialized similar to v3, v3+1 and v3+2, then no error occurs, But if I use scanf or gets (string is entered from keyboard), then error occurs. Error occurs whn program reaches up to point of printf for *(v3+3) and *(v3+4) in function test1. I could not understand, where I am making a mistake? Please help me in this matter.
    Code:
    #include<stdio.h>
    #include<conio.h>
    
    void test1(char **v3)
    {
         *v3="qk[1]";
         *(v3+1)="qf[k2]";
         *(v3+2)="qd[kk3]";
         
         scanf("%s",(v3+3));
         scanf("%s",(v3+4));
         //gets(*(v3+3));
         //gets(*(v3+4));
         
         printf("\n\n  Inner most fun:\n----------------------\n  Strings");
         printf("\n %s       %s        %s",*v3,*(v3+1),*(v3+2));
         printf("\n %s       %s",*(v3+3),*(v3+4));
         printf("\n  Address of strings:");
         printf("\n %d     %d     %d",v3,(v3+1),(v3+2));
         printf("\n %d     %d",(v3+3),(v3+4));
         printf("\n-----------------------------------------------\n");
         
         return;
    }
    
    void test2(char **v2)
    {
         test1(v2);
         printf("\n\n  Second fun:\n-------------------\n  Strings");
         
         printf("\n %s       %s        %s",*v2,*(v2+1),*(v2+2));
         printf("\n %s       %s",*(v2+3),*(v2+4));
         printf("\n  Address of strings:");
         printf("\n %d     %d     %d",v2,(v2+1),(v2+2));
         printf("\n %d     %d",(v2+3),(v2+4));
         printf("\n-----------------------------------------------\n");
         return;
    }
    
    main()
    {
          char **tst_str, *tst_elem[10];
          int i;
          
          printf("\n\n  Double pointer passing test\n---------------------------\n");
          tst_str=&tst_elem[0];
          printf("\n   Add-1           Add-2 ");
          printf("\n   (double ptr)    (&str array)");
          for(i=0; i<=4; i++)
               printf("\n    %-14d  %-14d",(tst_str+i), &tst_elem[i]);
          
          printf("\n    Before passing completed....\n");
          
          test2(tst_str);
          
          printf("\n\n  After all fun:\n  Strings");
          printf("\n %s       %s        %s",*tst_str,*(tst_str+1),*(tst_str+2));
          printf("\n %s       %s",*(tst_str+3),*(tst_str+4));
          printf("\n  Address of strings:");
          printf("\n %d     %d     %d",tst_str,(tst_str+1),(tst_str+2));
          printf("\n %d     %d",(tst_str+3),(tst_str+4));
          printf("\n\n  Direct acess of array");
          printf("\n %s       %s        %s",tst_elem[0],tst_elem[1],tst_elem[2]);
          printf("\n %s       %s",tst_elem[3],tst_elem[4]);
          
          printf("\n-----------------------------------------------\n");
          
          getch();
          
          return(0);
    }
    Thanks and regards....

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,560
    You may want to review the documentation for the scanf() and printf() functions. When I compile your code I get these warnings:
    main.c||In function ‘test1’:|
    main.c|10|warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char **’|
    main.c|11|warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char **’|
    main.c|19|warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘char **’|
    main.c|19|warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘char **’|
    main.c|19|warning: format ‘%d’ expects type ‘int’, but argument 4 has type ‘char **’|
    main.c|20|warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘char **’|
    main.c|20|warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘char **’|
    main.c||In function ‘test2’:|
    main.c|34|warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘char **’|
    main.c|34|warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘char **’|
    main.c|34|warning: format ‘%d’ expects type ‘int’, but argument 4 has type ‘char **’|
    main.c|35|warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘char **’|
    main.c|35|warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘char **’|
    main.c||In function ‘main’:|
    main.c|50|warning: format ‘%-14d’ expects type ‘int’, but argument 2 has type ‘char **’|
    main.c|50|warning: format ‘%-14d’ expects type ‘int’, but argument 3 has type ‘char **’|
    main.c|60|warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘char **’|
    main.c|60|warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘char **’|
    main.c|60|warning: format ‘%d’ expects type ‘int’, but argument 4 has type ‘char **’|
    main.c|61|warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘char **’|
    main.c|61|warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘char **’|
    You should also check your compiler settings to insure warnings are being generated.

    Jim

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,309
    It doesn't compile well. I get a few errors that are due to us using different compilers, but I get a ton of warnings. You should turn up the warnings on your compiler, or get a better one. Here's what I get when I compile your code:
    Code:
    $ gcc -Wall pointer.c
    pointer.c:2:18: error: conio.h: No such file or directory
    pointer.c: In function ‘test1’:
    pointer.c:10: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char **’
    pointer.c:11: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char **’
    pointer.c:19: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘char **’
    pointer.c:19: warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘char **’
    pointer.c:19: warning: format ‘%d’ expects type ‘int’, but argument 4 has type ‘char **’
    pointer.c:20: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘char **’
    pointer.c:20: warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘char **’
    pointer.c: In function ‘test2’:
    pointer.c:34: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘char **’
    pointer.c:34: warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘char **’
    pointer.c:34: warning: format ‘%d’ expects type ‘int’, but argument 4 has type ‘char **’
    pointer.c:35: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘char **’
    pointer.c:35: warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘char **’
    pointer.c: At top level:
    pointer.c:41: warning: return type defaults to ‘int’
    pointer.c: In function ‘main’:
    pointer.c:50: warning: format ‘%-14d’ expects type ‘int’, but argument 2 has type ‘char **’
    pointer.c:50: warning: format ‘%-14d’ expects type ‘int’, but argument 3 has type ‘char **’
    pointer.c:60: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘char **’
    pointer.c:60: warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘char **’
    pointer.c:60: warning: format ‘%d’ expects type ‘int’, but argument 4 has type ‘char **’
    pointer.c:61: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘char **’
    pointer.c:61: warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘char **’
    pointer.c:68: warning: implicit declaration of function ‘getch’
    1. You should be explicit and use int main(void). Good job on the return 0; though, most noobies forget that one.
    2. The conio.h and getch messages suggest you're using Turbo C. I'd recommend upgrading to something much better that's also free, like Pelles C.
    3. You can also drop conio.h and replace getch() with getchar() for a more portable version.
    4. Read up on the usage of scanf and printf and look up some examples. You're using the wrong format specifiers with the wrong types all over the place.
    5. test_elem contains 10 pointers, but they don't point anywhere yet. You need to allocate memory for each of them first, before you can safely read from or write to them.

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    19
    Thanks Anduril and Jim for helping,

    The conio.h and getch messages suggest you're using Turbo C
    . I use Dev C++ on windows, only for checking this stuff, I also used my old Turbo-C compiler. The major problem in program (apart form scanf/ printf argument, which was corrected) was about the allocation of memory for strings pointed by &test_elem[i], which was not allocated in program.

    Amal.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,309
    Both Dev-C++ and Turbo C are outdated. Dev-C++ hasn't been maintained for 5 or more, Turbo C for even longer. Turbo C especially, since it's an old 16-bit compiler, has a number of issues. Integer types are atypical for a modern compiler, it has trouble with the Windows' long file names, it uses old DOS memory models, and can only leverage a small amount of memory compared to a proper 32- or 64-bit program. All that, and it doesn't comply with the current C standard (C99). There is no reason to continue using either of those pieces of software when there are so many currently maintained and free alternatives. If you do C and C++, look into the Code::Blocks IDE, which comes with the MinGW (GCC port for Windows) compiler by default. Or, if you're only programming straight C, use Pelles C. MS Visual Studio Express is a third option that supports C and C++, though I don't recommend it's use for C since MS refuses to comply with the C99 standard.

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    19
    Thanks anduril .......I will try Code::Blocks .....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Double liked list and double pointer
    By saillesh.sabari in forum C Programming
    Replies: 1
    Last Post: 12-10-2010, 10:03 AM
  2. Replies: 4
    Last Post: 10-31-2009, 07:18 PM
  3. Replies: 3
    Last Post: 10-30-2009, 04:41 PM
  4. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  5. Want to now what is the use of double pointer
    By ammu in forum C Programming
    Replies: 3
    Last Post: 11-10-2008, 11:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21