开发者

dynamic pointer value allocation

I have a pointer pointing to an array which is filled dynamically at run time. After collecting and storing the array, I want all the remaining buffer locations of the array to be filled with empty space. How can I do that?


From some comments:

Here is what I have:

 char buf[50];
 char *ptr = buf;
 strncpy(ptr, info.a, strlen(info.a));
 ptr += strlen(info.a);
 strncpy(ptr, info.b, strlen(info.b));
 ptr += strlen(info.b);
 strncpy(ptr, 开发者_运维知识库info.c, strlen(info.c));
 ptr += strlen(info.c);

How do I fill the remaining pointer locations with ' '?


You can use memset(3) to fill an area of memory with spaces:

size_t total_size = get_total_size(); // total size of array, in bytes
size_t len = get_len(); // length of content, in bytes, <= total_size
assert(len <= total_size);
char *array = malloc(total_size);
// ... fill the first len bytes with your data
memset(&array[len], ' ', total_size - len); // and the rest to ' ' chars

There are a couple problems with an approach like this though. The first is that you're vulnerable to buffer overflows unless you carefully check that len < total_size. Second, it sounds like you're going to use this as a string, in which case you'll want to take care to leave a trailing null '\0' character.


This is what I interpreted from your question

int Array[20];
int *p=Array;
int no,i,len;
char ch=' '; 
len=sizeof(Array)/sizeof(int)
printf("Enter no of elements "); 
scanf("%d",&no);
for(i=0;i<no;i++)
    scanf("%d",p+i);   
for(i=no;i<len;i++)
    p[i]=(int )ch;

Hope this helps.


I think that the cleanest way to do this is right after you allocate the array and assign ptr to the beginning of buf. Just fill everything with ' ' using memset().

 char buf[50];
 char *ptr = buf;
 memset (ptr, ' ', sizeof(buf)); // like this

 // If this buffer is meant to be printed to the screen or used as a string,
 // it's probably better to write the end-of-string character at the last 
 // position of the buffer:
 buf[49] = 0;

 // operations to write stuff on the buffer

So at the end of your operations, all the remaining locations of the buffer which haven't been used will have ' '.

EDIT:

I've been thinking about what you asked and unless you have very specific reasons to fill the array with empty spaces that's not usually how we deal with this situation (check my comment below this answer).

char* tmp_string = "**Copy Me**";
printf("Size of string:%d\n", strlen(tmp_string));

char buf[50];
printf("Size of buf:%d\n", sizeof(buf));
memset(buf, 0, sizeof(buf)); // cleaning the entire buffer

char *ptr = buf;
strncpy(ptr, tmp_string, strlen(tmp_string));
ptr += strlen(tmp_string);
*ptr = '!';

printf("Size of buf after copy: %d\n", strlen(buf));
printf("Result: %s\n", buf);

Outputs:

Size of string:11
Size of buf:50
Size of buf after copy: 12
Result: **Copy Me**!

Notice that the size of the buf after copy is 12. That's because we have zeroed the buffer in the beginning of the operations (i.e. filled the buffer with \0). At the first sign of \0 in the buffer, printf() stops iterating on the array and printing characters.

HOWEVER, I can think of one good reason of why you don't want this method. The following is a slight modification of the code above. It fills the buffer with empty spaces and adds the end-of-string character (\0) at the last position of the buffer. Notice that the size of the string in the end is not 12! It's 49 because you filled the buffer with valid characters.

char* tmp_string = "**Copy Me**";
printf("Size of string:%d\n", strlen(tmp_string));

char buf[50];
printf("Size of buf:%d\n", sizeof(buf));
memset(buf, ' ', sizeof(buf)); // cleaning the entire buffer with empty spaces
buf[49] = 0; // setting a \0 at the end of the string

char *ptr = buf;
strncpy(ptr, tmp_string, strlen(tmp_string));
ptr += strlen(tmp_string);
*ptr = '!';

printf("Size of buf after copy: %d\n", strlen(buf));

printf("Result: %s\n", buf);

Outputs:

Size of string:11
Size of buf:50
Size of buf after copy: 49
Result: **Copy Me**!
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜