0

I want to create a new array with the same size of "chaine" but only after the function

 char chaine[Lenght];   //Lenght = 20
 function(chaine, sizeof(chaine));

When I called "function" the size of "chaine" is changing randomly.

The new array "chaine2" also needs to be full of " * " characters.

Let met try to explain with some printf :

     printf("chaine = %s\n", chaine);

will show on screen something like : chaine = WORDS (5 characters)

And i want "chaine2" to be shown like this : chaine2 = ***** (5 stars)

I apologize for my english, thank you for reading

1
  • 1
    You did not initialize chaine and you failed to show the code in function. Commented Apr 26, 2012 at 2:20

3 Answers 3

2
#include <stdio.h>
#include <stdlib.h>

char *dup_and_fillStar(const char *src, const size_t size){
    char *p,*ret;
    ret=p=(char*)malloc(sizeof(char)*size);
    while(*src++)
        *p++='*';
    *p = '\0';
    return ret;
}

#define Length  20

int main(void){
    char chaine[Length] = "WORDS"; 
    char *chaine2;

    chaine2 = dup_and_fillStar(chaine, sizeof(chaine));
    printf("chine = %s\n", chaine);
    printf("chine2 = %s\n", chaine2);
    free(chaine2);

    return(0);
}
Sign up to request clarification or add additional context in comments.

Comments

1

Remember that char arrays are special, in the sense that they have a size, which you specify when you declare them, and a length, which depends on their contents. The size of an array is the amount of memory that's been allocated to it. The length of a string is the number of characters before a terminating null ('\0').

some_func() {
  int len = 20;      // Size of the array
  char chaine[len];  // Uninitialized array of size 20.

  memset(chaine, '\0', sizeof(chaine)); // Init to all null chars, len = 0
  strcpy(chaine, "WORDS"); // Copy a string, len = 5

  char *chaine2 = function(chaine, sizeof(chaine));
  printf("%s\n", chaine2);
  free (chaine2);
}

When you pass an array to a function, it's treated like a pointer. So sizeof(str) inside the function will always return the size of pointer-to-char, and not the size of the original array. If you want to know how long the string is, make sure it's null-terminated and use strlen() like this:

char *function(char *str, int len) {
  // Assume str = "WORDS", len = 20.
  char *new_str = malloc(len); // Create a new string, size = 20

  memset(new_str, '\0', len);  // Initialize to nulls
  memset(new_str, '*', strlen(str)); // Copy 5 '*' chars, len = 5

  return new_str;  // Pointer to 20 bytes of memory: 5 '*' and 15 '\0'
}

Comments

1

I'm going to assume that you're using C99 (and so are able to use variable length arrays).

If you want to create the array outside of function() (where chaine is still an array), you can simply do:

char chaine2[Lenght];  

Or, probably better:

char chaine2[sizeof(chaine)];

If you're inside function(), note that chaine will be a pointer, since the compiler sees an array definition in a function parameter as a pointer to it's first element (if this is confusing, remember that pointers are not arrays)

So, even if function is defined like this:

function(char chaine[], size_t length) {

the compiler will see it as:

function(char *chaine, size_t length) {

Either way, you can create chaine2 by saying:

    char chaine2[length];

but NOT

    char chaine2[sizeof(chaine)]; // DONT DO THIS

since the sizeof will return the size of the pointer instead of the size of the array.

However you create it, if you want to fill the new array with '*' characters, you can use memset():

 memset(chaine2,'*',sizeof(chaine2));

1 Comment

Remember that chaine2[] is allocated on the stack, so it will become invalid when the function returns.

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.