Thread: Need some explanation conserning this code, (char arrays and pointers)

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    61

    Need some explanation conserning this code, (char arrays and pointers)

    Greetings EveryOne

    Look at this code;

    Code:
    #include <stdio.h>
    
    char StringA[] = "Hello, I am Laythe.";
    char StringB[30];
    
    int main()
    {
        char *PointerA;
        char *PointerB;
    
        printf("puts(StringA) =\n");
        puts(StringA);  // Prints StringA on the screen;
        printf("\n");
    
        PointerA = StringA;
    
        printf("puts(PointerA) = \n");
        puts(PointerA);
        printf("\n");
    
        PointerB = StringB; // Line 21;
    
        while(*PointerA != '\0')
        {
            *PointerB++ = *PointerA++;
        }
        *PointerB = '\0';
    
        printf("puts(StringB) = \n");
        puts(StringB);
        printf("\n");
    
        PointerB = StringB; // Line 33;
    
        printf("puts(PointerB) = \n");
        puts(PointerB);
    
        return 0;
    }
    - At line 21, if you comment it or delete it, the compiler will stop compiling at the line.
    why is that?

    * don't forget to return line 21 as it was if you modified it.

    - At line 33, if you comment it or delete it, the value of PointerB is null.
    why is that?

    * compile the same code with some modifications and you will get a run time error, can you tell me why?

    Code:
    #include <stdio.h>
    
    char StringA[] = "Hello, I am Laythe.";
    char StringB[30];
    
    int main()
    {
        char *PointerA;
        char *PointerB;
    
        printf("puts(StringA) =\n");
        puts(StringA);  // Prints StringA on the screen;
        printf("\n");
    
        PointerA = StringA;
    
        printf("puts(PointerA) = \n");
        puts(PointerA);
        printf("\n");
    
        PointerB = StringB; // Line 21;
    
        while(*PointerA != '\0')
        {
            *PointerB++ = *PointerA++;
        }
        *PointerB = '\0';
    
        printf("puts(StringB) = \n");
        puts(StringB);
        printf("\n");
    
        PointerB = StringB; // Line 33;
    
        printf("puts(PointerB) = \n");
        puts(PointerB);
        printf("\n");
        printf("*PointerB = %s", *PointerB);
    
        return 0;
    }
    Thank You In Advance

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    This is all C, isn't it? Yet, why is it in the C++ section?
    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
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Elysia View Post
    This is all C, isn't it? Yet, why is it in the C++ section?
    Because for the moment there seems to be a bug in the forum software...

    When you click on "C Programming" you end up in the C++ section...

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Ouch. Hadn't noticed.
    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.

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    61
    yes am absolutely facing that bug in the forum.
    trying to post in C forum but ending up on C++ forum.

    anyway, might someone help me with my problem please.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > - At line 21, if you comment it or delete it, the compiler will stop compiling at the line.
    > why is that?
    Have you tried it with your compiler?
    Did you get any messages?
    Did it actually stop compiling (with an error, as opposed to a warning)?


    > - At line 33, if you comment it or delete it, the value of PointerB is null.
    > why is that?
    That depends, does your tutor know the difference between nul and NULL?
    ptr == NULL is one thing
    *ptr == '\0' is a completely different thing.
    Which do you mean?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Dec 2010
    Posts
    61
    > - At line 21, if you comment it or delete it, the compiler will stop compiling at the line.
    > why is that?
    Have you tried it with your compiler?
    Did you get any messages?
    Did it actually stop compiling (with an error, as opposed to a warning)?
    yes i have tried it with my compiler of course
    i get no error or warning, when the code is run it stops outputting at line 21 and getting this in the building log: Process terminated with status -1073741819 (0 minutes, 47 seconds).
    *instead of getting "Process terminated with status 0"

    > - At line 33, if you comment it or delete it, the value of PointerB is null.
    > why is that?
    That depends, does your tutor know the difference between nul and NULL?
    ptr == NULL is one thing
    *ptr == '\0' is a completely different thing.
    Which do you mean?
    i am self-tutoring, and i have read about the difference between NULL and nul.
    why don't u run this and see what i'm talking about concerning NULL.

    Code:
    #include <stdio.h>
    
    char StringA[] = "Hello, I am Laythe.";
    char StringB[30];
    
    int main()
    {
        char *PointerA;
        char *PointerB;
    
        printf("puts(StringA) =\n");
        puts(StringA);  // Prints StringA on the screen;
        printf("\n");
    
        PointerA = StringA;
    
        printf("puts(PointerA) = ");
        puts(PointerA);
        printf("\n");
    
        PointerB = StringB; // Line 21;
    
        while(*PointerA != '\0')
        {
            *PointerB++ = *PointerA++;
        }
        *PointerB = '\0';
    
        printf("puts(StringB) = \n");
        puts(StringB);
        printf("\n");
    
        //PointerB = StringB; // Line 33;
    
        printf("puts(PointerB) = ");
        puts(PointerB);
        //printf(*PointerB);
        printf("\n");
        printf("*PointerB = %s", *PointerB);
    
        return 0;
    }
    P.S: as I mentioned, I'm self-learning to program so I'm analysing everything I read and code so I can have a deep understanding of programming.

    Any help would be so very much appreciated cos the only tutor I can have is articles from the net and your help on the forum, Thank You.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    I see you're confusing compile errors, build errors, and run-time errors.

    What you have is a run-time error (at line 21)

    > -1073741819
    In hex, this is 0xC0000005 (google it)
    It is the "Access violation" exception in windows, and it basically means you tried to access some memory you didn't own.
    One VERY common way of getting this is to try to use an UNINITIALISED pointer.
    Since your line 21 initialises a pointer, you can now see what happens.


    Your line 33 is either leaving pointerb pointing to the \0 at the end of the string (so you print nothing), or you reset it back to the start of the array.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char pointers and arrays
    By Scramble in forum C Programming
    Replies: 3
    Last Post: 06-30-2010, 04:17 AM
  2. Pointers & char arrays
    By lautarox in forum C Programming
    Replies: 11
    Last Post: 05-13-2009, 08:49 PM
  3. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. use of pointers with char arrays
    By txp200 in forum C Programming
    Replies: 6
    Last Post: 05-12-2004, 06:52 PM