Thread: pointers

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    38

    pointers

    hello,

    i know its a basic question on pointers/malloc , but i wud still like if u can explain y in the following code we need to allocate memory to str2 and str3 be4 using strcpy and memcpy??

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    
    int main()
    {
    
    
    char str1[]="This is for testing";
    char *str2;
    char *str3;
    
    
    strcpy(str2,str1); //Copies the contents of str1 to str2
    
    
    memcpy(str3,str1,strlen(str1)); //Copies the contents of str1 to str3
    
    
    printf("String 2 using strcpy(): %s\nString 3 using memcpy(): %s\n",str2,str3);
    
    
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ueg1990e
    i wud still like if u can explain y in the following code we need to allocate memory to str2 and str3 be4 using strcpy and memcpy?
    Okay, I have a box which has the label "Box A" written on a sticker that is pasted on the box. I also have a sticker on which is written "Box B", but I don't have any other boxes at the moment. I want to transfer the marbles from Box A to Box B. Can you explain why I need to buy another box (and paste the second sticker on it first)? After all, I have the "Box B" sticker, right?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Whilst str1 is a useful container,

    str2 is just this
    pointers-deep_pit-jpg
    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.

  4. #4
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    The analogy I usually use (actually have used since I had to explain pass-by-value and pass-by-reference to my own CS teacher...) is signposts. A pointer is a signpost to something. It isn't necessarily the ONLY signpost pointing to something, and it's not necessarily pointing to anything at all! If you don't use it properly, you *WILL* get lost!

    This line creates a box called str1 that we can put something in, and then puts the string into it:
    Code:
    char str1[]="This is for testing";
    This line, however, creates only a signpost. At the moment it points in a random direction because we haven't told it to point to ANYTHING yet. There is not necessarily ANYTHING at the end of where it points:
    Code:
    char *str2;
    This line says to copy the contents of box str2 to str1. str2 is a box, so that's fine. But str1 is actually NOT a box. It's a signpost. But it doesn't even POINT at a box either, because we didn't initialise it. So this will, in effect, copy str2 into a random part of memory that we have no idea where it is. The second it does that, the operating system will kick up fuss and security warnings (most likely a segfault in the program) because we probably aren't allowed to write stuff where that signpost is pointing anyway.
    Code:
    strcpy(str2,str1);
    The fix, of course, would be to either use a box instead:

    Code:
    char str2[20];
    or make the signpost point at a box:

    Code:
    char allocated_box[20];
    char *str2 = allocated_box;
    (where you get the box from is not relevant, here we have a "static" box the same kind as str1, but you could also be given a "dynamic" box by malloc, for example).

    But without the box, the signpost is useless. It's effectively saying "It's over there" when "It" doesn't even exist.

    Pointers are signposts. They are not, in and of themselves, containing anything. They just point to somewhere else that (hopefully) contains what you want. When you pass a pointer to a function, you're NOT passing the data inside the box to the function - you're just telling the function where it can find the box (pass by reference)

    Similarly, if you pass a "box" to a function, you are passing ALL the data inside it to the function (which may cause problems if you're passing MILLIONS of items in an array, for example - pass by value).

    Also, if you remove the box but not the signpost that's pointing at it, you will run into trouble (variable scoping, premature free'ing etc.).

    Additionally, if you set one hundred signposts to point at the same box, it does not give you 100 copies of the data. They are all "looking" at the same box. Changing any of the boxes contents will mean that EVERY signpost will still point to that same box.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    35
    @ledow looks like you got to interchange the positions of str2 and str1 in this paragraph
    This line says to copy the contents of box str2 to str1. str2 is a box, so that's fine. But str1 is actually NOT a box. It's a signpost. But it doesn't even POINT at a box either, because we didn't initialise it. So this will, in effect, copy str2 into a random part of memory that we have no idea where it is. The second it does that, the operating system will kick up fuss and security warnings (most likely a segfault in the program) because we probably aren't allowed to write stuff where that signpost is pointing anyway.

  6. #6
    Registered User
    Join Date
    Jan 2012
    Posts
    38
    thnx alot guys.....that was really helpful!!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. size of struct with pointers and function pointers
    By sdsjohnny in forum C Programming
    Replies: 3
    Last Post: 07-02-2010, 05:19 AM
  2. Storing function pointers in generic pointers
    By Boxknife in forum C Programming
    Replies: 6
    Last Post: 08-01-2009, 01:33 PM
  3. Variable pointers and function pointers
    By Luciferek in forum C++ Programming
    Replies: 11
    Last Post: 08-02-2008, 02:04 AM
  4. Pointers to objects -- passing and returning pointers
    By 1veedo in forum C++ Programming
    Replies: 4
    Last Post: 04-04-2008, 11:42 AM
  5. weak pointers and use_count smart pointers
    By Mario F. in forum C++ Programming
    Replies: 2
    Last Post: 07-29-2006, 07:54 AM