0

I radically re-edited the question to explain better my application, as the xample I made up wasn't correct in many ways as you pointed out:

I have one pointer to char and I want to copy it to another pointer and then add a NULL character at the end (in my real application, the first string is a const, so I cannot jsut modify it, that's why I need to copy it).

I have this function, "MLSLSerialWriteBurst" which I have to fill with some code adapt to my microcontroller.

tMLError MLSLSerialWriteBurst( unsigned char slaveAddr, 
                           unsigned char registerAddr, 
                           unsigned short length, 
                           const unsigned char *data )
{
unsigned char *tmp_data;

  tmp_data = data;
  *(tmp_data+length) = NULL;

  // this function takes a tmp_data which is a char* terminated with a NULL character ('\0')
  if(EEPageWrite2(slaveAddr,registerAddr,tmp_data)==0)
    return ML_SUCCESS;
  else 
    return ML_ERROR;
}

I see there's a problem here: tha fact that I do not initialize tmp_data, but I cannot know it's length.

9
  • 6
    Your code shows a deep lack of understanding of how pointers work. I'd advise you to read a good book on C programming first. Commented Jan 9, 2011 at 17:00
  • 1
    It would be helpful if you showed what you initialized from_string and to_string to. Commented Jan 9, 2011 at 17:01
  • Also, you should have a statement which null terminates the "caio" string: *(from_string+4) = '\0'; Commented Jan 9, 2011 at 17:03
  • Also, show what your output is. That would help us answer this question. Commented Jan 9, 2011 at 17:05
  • 1
    Read this pw1.netcom.com/~tjensen/ptr/pointers.htm tutorial first. It will help you figure out what pointers and strings in C are. Ask again if you do not understand something in the tutorial. Commented Jan 9, 2011 at 17:12

2 Answers 2

3

For starters, you are missing a bunch of declarations in your code. For example, what is lungh? Also, I'm assuming you initialized your two pointers so they point to memory you can use. However, maybe that's not a safe assumption.

Beyond that, you failed to terminate your from string. So getting the length of the string will not work.

There seems to be numerous errors here. It's hard to know where to start. Is this really what your actual code looks like? I don't think it would even compile.

Finally, there seems to be a bit of confusion in your terminology. Copying a pointer is different from copying the memory being pointed to. A pointer is a memory address. If you simply copy the pointer, then both pointers will refer to the same address.

I would create a copy of a string using code similar to this:

char *from_string = "ciao";
char *to_string;
int len;

len = strlen(from_string);
to_string = (char *)malloc(len + 1);
if (to_string != NULL)
    strcpy(to_string, from_string);
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you Jonathan, but I cannot understand the need of if (to_string != NULL)
@Stefano, in case malloc() failed to allocate memory it returns 0
@Stefano: Well the memory needs to come from somewhere, even if you just do char *to_string = "_____". The only problem is you don't know how much memory is required until you test the from string at run time. I'm pretty sure your system has some way to allocate a few bytes.
0

Be fully aware that you do not want to copy a pointer. You want to copy the memory that is pointed to by the pointer. It does sound like you should learn more about pointers and the memory environment of your system before proceeding too much farther.

When you say tmp_data = data, you are pointing tmp_data to the same memory pointed to by data. Instead, you need to allocate a new block of memory and copy the memory from data into it.

The standard way to do this is with malloc. If you do not have malloc, your libraries may have some other way of acquiring a pointer to usable memory.

unsigned char * tmp_data = malloc(length + 1);
if(tmp_data != 0) {
    memcpy(tmp_data, data, length);
    tmp_data[length] = 0;
    // ...
    free(tmp_data);
}

You could also use a fixed-size array on the stack:

unsigned char tmp_data[256];
if(length >= sizeof(tmp_data)) length = sizeof(tmp_data) - 1;
memcpy(tmp_data, data, length); // or equivalent routine
tmp_data[length] = 0;

C99 introduced variable-length arrays, which may be what you seek here, if your compiler supports them:

unsigned char tmp_data[length];
memcpy(tmp_data, data, length); // or equivalent routine
tmp_data[length] = 0;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.