Thread: Set a pointer's value without changing it's address?

  1. #1
    Registered User
    Join Date
    Jan 2019
    Posts
    5

    Set a pointer's value without changing it's address?

    Hello I'm very new to C and I'm doing practice problems to improve my understanding. I've been stuck on a question about dynamic memory allocation and pointers, I want to set the value at ptr's to the string "12345678" but I dont want to alter ptr's memory address. I've tried the following methods but I always end up changing its address. What would be the right way to do something like this?

    Code:
    char *ptr = malloc(20*(sizeofchar));
    ptr="12345678";
    Code:
    char *ptr = malloc(20*(sizeofchar));
    char *x=&ptr;
    x="12345678";

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,451
    Have you tried strcpy()?

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    1,646
    C / C++ don't allow array assignment. Structures can be used:

    Code:
    typedef struct{
    char str[20];
    }mystruct;
    
    
    int main()
    {
        mystruct st1 = {"structure 1"};
        mystruct st2 = {"structure 2"};
        mystruct *stp = &st1;
        *stp = st2;   // replaces st1.str with "structure 2", stp is not changed
        return 0;
    }
    Last edited by rcgldr; 01-28-2019 at 01:45 PM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,164
    rcgldr: that won't work because mfr explicitly wants to use dynamic memory allocation. jimblumberg's suggestion is probably along the lines of what mfr was looking for.
    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

  5. #5
    Registered User
    Join Date
    Apr 2013
    Posts
    1,646
    Quote Originally Posted by laserlight View Post
    rcgldr: that won't work because mfr explicitly wants to use dynamic memory allocation.
    In that case:

    Code:
    typedef struct{
    char str[20];
    }mystruct;
    
    
    int main()
    {
        mystruct *stp = (mystruct *)malloc(sizeof(mystruct));
        *stp = (mystruct) { "12345678" };  // stp->str changes, stp does not change
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 08-24-2015, 07:43 AM
  2. changing the address of a pointer question..
    By transgalactic2 in forum C Programming
    Replies: 42
    Last Post: 10-16-2008, 09:20 AM
  3. Changing IP Address Programmatically
    By jaro in forum Tech Board
    Replies: 5
    Last Post: 05-03-2007, 03:56 AM
  4. Changing memory address
    By tzpb8 in forum C Programming
    Replies: 3
    Last Post: 07-26-2006, 09:50 AM
  5. Should i pass address of pointer or just pointer???
    By howhy in forum C++ Programming
    Replies: 11
    Last Post: 09-02-2005, 04:05 AM

Tags for this Thread