Thread: help

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    96

    help

    why is this not printing anything on the screen
    Code:
    #include<stdio.h>
    #include<conio.h>
    
    int fun1 (int a[], int *b);
    
    main()
     {
          char name[15]={'j','o','h','n'};
          char *ptr[3];
                  
          ptr[1]=name;
          printf("%c", *(ptr+2));   
                             getch();
    };

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Why do you think it will? *(ptr + 2) is not initialized (otherwise known as ptr[2]).
    Furthermore, as you see, you are trying to print a string (char*) with %c.
    And finally, choose a proper subject next time. Not just "help".
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Only ptr[1] points to anything except garbage locations. On three runs of your program, it printed up a $, an 'l', and nothing.

    Try this version of your program:

    Code:
    #include<stdio.h>
    #include<conio.h>
    
    int fun1 (int a[], int *b);
    
    main()
     {
          char name[15]={'j','o','h','n'};
          char *ptr[3];
                  
          ptr[1]=name;
          printf("\n ptr[1] now points to: %s\n", ptr[1]);
          printf("%c", ptr[1][2]);   
          getch();
    };

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by Adak View Post
    Only ptr[1] points to anything except garbage locations. On three runs of your program, it printed up a $, an 'l', and nothing.

    Try this version of your program:

    Code:
    #include<stdio.h>
    #include<conio.h>
    
    int fun1 (int a[], int *b);
    
    main()
     {
          char name[15]={'j','o','h','n'};
          char *ptr[3];
                  
          ptr[1]=name;
          printf("\n ptr[1] now points to: %s\n", ptr[1]);
          printf("%c", ptr[1][2]);   
          getch();
    };
    Yikes. Don't forget to terminate your name string before you try printing it.

    Use this line instead:
    Code:
    char name[15] = { 'j', 'o', 'h', 'n', '\0' };
    If you understand what you're doing, you're not learning anything.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Ah, another problem joins the fray.
    It would be better to do
    char name[] = "john";
    or
    char name[15] = "john";
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by itsme86 View Post
    Yikes. Don't forget to terminate your name string before you try printing it.

    Use this line instead:
    Code:
    char name[15] = { 'j', 'o', 'h', 'n', '\0' };
    You know what, actually Adak's solution would have worked since the array size was specified it would fill any non-initialized elements with 0s. My bad.
    If you understand what you're doing, you're not learning anything.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by itsme86 View Post
    You know what, actually Adak's solution would have worked since the array size was specified it would fill any non-initialized elements with 0s. My bad.
    No actually it would work because it was only printing a single character (the 'h'), not a string.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by iMalc View Post
    No actually it would work because it was only printing a single character (the 'h'), not a string.
    Really? What do you call this line of code?
    Code:
          printf("\n ptr[1] now points to: %s\n", ptr[1]);
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed