1

Hey guys I get a really strange thing going on in my for loop.

When I execute this code here:

#include <stdio.h>
#include <string.h>    
char* repeat(char c, int n);

int main(void)
{
    char* input;
    input = repeat('c', 12);

    return 0;
}

char* repeat(char c, int n)
{
    char* out;

    for (int i = 0; i < 12; ++i) //FIX ITERATION
    {
        int len = strlen(out);
        out[len] = c;
        out[len+1] = '\0';
    }

    printf("%s\n", out);
    return out;
}

I get the expected output:

cccccccccccc

But when I use the passed int in my method, like this:

char* repeat(char c, int n)
{
    char* out;

    for (int i = 0; i < n; ++i) //VARIABLE ITERATION
    {
        int len = strlen(out);
        out[len] = c;
        out[len+1] = '\0';
    }

    printf("%s\n", out);
    return out;
}

I just get this as output:

cccc

Please tell me what am I doing wrong. I don't have any clue what the error could be?

thank's for helping!

3 Answers 3

6

This line is the problem

  int len = strlen(out);
  out[len] = c;
  out[len+1] = '\0';

out is not initialized. You did not allocate memory in this statement:

char* out;

So you are experiencing Undefined Behavior.

Section 3.4.3

1 undefined behavior

behavior, upon use of a nonportable or erroneous program construct or of erroneous data, for which this International Standard imposes no requirements

Section 4.1:

An lvalue (3.10) of a non-function, non-array type T can be converted to an rvalue. If T is an incomplete type, a program that necessitates this conversion is ill-formed. If the object to which the lvalue refers is not an object of type T and is not an object of a type derived from T, or if the object is uninitialized, a program that necessitates this conversion has undefined behavior. If T is a non-class type, the type of the rvalue is the cv-unqualified version of T. Otherwise, the type of the rvalue is T.

In both examples you have shown the result is undefined.

You have to allocate memory:

char * out = malloc(sizeof(char)*50); // i have used size 50 - take sufficient what you need
//initialize it 
out[0] = '\0';

Make sure to include stdlib.h.

out now points to a memory block which can hold 50 chars.

8
  • Thank you for helping. How would you initialize out in this case. like: char* out = &out[0]? I am pretty new in C. Jan 8, 2015 at 6:53
  • @user3025417 The size you provide depends on your requirement - see my edit.
    – Sadique
    Jan 8, 2015 at 6:55
  • @user3025417: use a memset
    – eckes
    Jan 8, 2015 at 6:55
  • @eckes: indeed you allocated memory for out but you still didn't initialize it - passing out that way to strlen is still not good idea I think Jan 8, 2015 at 6:57
  • @user300234: after your malloc, you memset the fresh out with memset(out, 0, sizeof(char)*50). Then, a strlen on the empty out will return 0 and a strlen on a modified out will return a correct value.
    – eckes
    Jan 8, 2015 at 7:00
3

In the function repeat(), you don't assign any memory for out to point to. Then you try to return the uninitialized pointer. That can be OK if you've dynamically allocated the memory, or if you've got it pointing to a static array, or it points to a string literal, but if it points to a local (automatic) array, it is bad news.

Your code is exhibiting undefined behaviour; any behaviour is valid because your code is malformed.

2
char* out;

is never initialized so using uninitialized variables lead to UB

You are using the uninitialized variable out in strlen(out)

The pointer out should point to some valid memory location .

char *out = malloc(size);
2
  • That gives me: warning: implicit declaration of function int malloc(...)' warning: initialization to char *' from `int' lacks a cast Jan 8, 2015 at 7:14
  • @user3025417 int malloc()?
    – Gopi
    Jan 8, 2015 at 8:12

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.