1

I'm relatively a beginner in programming in C and am getting super confused with arrays and pointers.

Basically what I'm trying to do is extend a string that contains binary to the designated length len; (i.e. len=8 for num[]=101 would produce "00000101").

Can someone help me understand what's wrong with this?

const char * extendBinary(char num[], int len) {
        char *number = #
        int length = len;
        int difference;
        if(strlen(*num)<len) {
            difference = len-strlen(num);
            while(difference>0)
            {
                &number = strcat("0", &number);
                difference--;
            }
        }
        return number;
    }
7
  • 3
    You need to do some easier experimentation with pointers and their operators, because honestly your code is messed up. Commented Apr 25, 2013 at 17:30
  • 1
    I realize that, but are there any obvious fixes that I'm not seeing? At least with the return type and parameters. Commented Apr 25, 2013 at 17:34
  • Are you getting an error or just wrong results? If it's the results, what are they? Commented Apr 25, 2013 at 17:37
  • 1
    In the return type you can probably drop the const. As for the arguments I'd recommend you to use size_t for sizes and lengths arguments, and for the num argument you have to remember that all arrays decays to pointers so in the argument declaration char num[] is the same as char *num (however char a[][X] is not the same as char **a). Commented Apr 25, 2013 at 17:38
  • 1
    you must getting a warning that char *number = &num; incomplete Commented Apr 25, 2013 at 17:39

4 Answers 4

1

Your problems start with your specification. If I understand you correctly, you want to have a function where you pass an array of characters and a length. The size of your array of input characters will be between 1 and len? However, your function has no way of knowing what the size of your array num is. If you wanted this to work, you would need to define your function as

const char * extendBinary(char *num, size_t num_len, int len);

so that your function doesn't overrun your buffer pointed to by num. Note that I replaced char num[] with char *num as this is the common mechanism for passing a pointer. You cant pass pointers to arrays and then dereference that pointer and get back the original type (that includes its size) -- that's just one thing that C doesn't let you do, so just use a normal pointer and a separate size variable.

Finally, you'll have to deal with memory allocation unless you want a memory leak. Thus, you could simply say that whom ever calls extendBinary should free it's return value when done with it.

const char * extendBinary(char *num, size_t num_len, int len) {
    char *ret = malloc(len + 1);
    int i;

    memset(ret, '0', len);
    ret[len] = 0;
    strncpy(&ret[len - num_len], num, num_len);

    return ret;
}

int main(void) {
    char arr[] = {'1', '0', '1'};
    const char *formatted = extendBinary(arr, sizeof(arr), 8);

    printf("%s\n", formatted);
    free(formatted);
    return 0;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Your explanation is perfect, and is helping me write the rest of my code. Thank you so much!
So glad to help! However, I omitted some bounds checking in the above code. Specifically, you should make sure that num_len is actually smaller than len and maybe just allocate a larger buffer if it is (plus one for the null-terminator). Sorry about that, I was trying to keep my answer concise.
0

this is wrong.

strcat("0", &number);

A weird way to fix you code would be this:

char temp[32] = {};

...
    ...
        while(difference>0)
        {
            strncat(temp, "0", 31 - strlen(temp));
            difference--;
        }
        strncat(temp, num, 31 - strlen(temp));
        strncpy(num, temp, len);

Note, I am writing this code just to help you understand how strcat() works, there is much better ways to do what you are trying to do.

You cannot concatenate something to a const string, you must have entire control of what is happening into you code, and where your code is writing. Do you know where is the pointer to "0" in your source?

Comments

0

How do you set up num? If it's really an array of characters rather than a string, there's no requirement that it be null terminated, unless it's a global/static. If you set it up like so in a function:

char str[10];
str[0] = '1';
str[1] = '0';
str[2] = '1';

than your strlen will get whatever, depending upon whatever junk happens to be in num.

Comments

0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//case 1: for num[9]="101";
char *extendBinary1(char num[], int len) {
    int num_len = strlen(num);
    memmove(num + (len - num_len), num, num_len);
    memset(num, '0', (len - num_len));

    return num;
}

//case 2: for "101";//pointer to const char 
char *extendBinary2(const char num[], int len) {
    int num_len = strlen(num);
    char *number = calloc(len + 1, sizeof(char));
    memset(number, '0', (len - num_len));

    return strcat(number, num);
}


int main(void){
    char num[9] = "101";
    char *number = extendBinary2("101", 8);//dynamic allocate

    printf("%s\n", extendBinary1(num, 8));
    printf("%s\n", number);//free(number);

    return 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.