1

I am running my C program on 64 bit linux. I have written two small functions to which I will pass a char array and it will be filled by the function and returned back.

I am facing some issues with the first function func1().

The function are func1:

void func1(char *str1)
{
    short optiony = 0;

    printf("\tfunc1: str1 at %u\n", str1);

    printf("\tProvide the option:\n");
    scanf("%d", &optiony);

    if(optiony== 1)
    {
        strcpy(str1, "168.98.173.44");
    }

    printf("\tfunc1: str1 at %u is %s\n", str1, str1);
}

func2:

void func2(char *str2)
{
    short optiony= 0;

    printf("\tfunc2: str2 at %u\n", str2);

    printf("Provide the option:\n");
    scanf("%d",&optiony);

    if(optiony== 1)
    {
        strcpy(str2, "168.97.176.146");
    }

    printf("\tfunc2: str2 at %u is = %s\n", str2, str2);
}

main:

main(int argc, char* argv[])
{
    char str1[10] = "\0";
    char str2[10] = "\0";

    printf("main: str1 at %u\n", &str1);
    func1(str1);
    printf("main: str1 at %u is %s\n\n", &str1, str1);

    printf("main: str2 at %u\n", &str2);
    func2(str2);
    printf("main: str2 at %u is %s\n", &str2, str2);

    return(0);
}

The output is:

main: str1 at 4149951616
        func1: str1 at 4149951616
        Provide the option:
1
        func1: str1 at 4149951616 is 168.98.173.44
main: str1 at 4149936112 is 
                   ^
                   |
                    --------------------------------note the change of address

main: str2 at 4149936096
        func2: str2 at 4149936096
        Provide the option:
1
        func2: str2 at 4149936096 is = 168.97.176.146
main: str2 at 4149936096 is 168.97.176.146

The problem occuring is in the main program after the call returns from func1() the address of the variable passed to it looks changed. Due to this the value set inside the function is not reflecting in main. The issue is not occuring in func2().

What is bug in this code?

6
  • 4
    printf addresses with the %p format specifier. And don't scanf shorts with %d, use int instead. Commented Jul 28, 2015 at 14:01
  • 2
    The strings are longer than the char []s. Commented Jul 28, 2015 at 14:02
  • 1
    Increase your warning level and you'll find lots of warnings due to using wrong format specifiers, leading to undefined behavior Commented Jul 28, 2015 at 14:03
  • 1
    @CoolGuy he probably user Turbo C++ which doesn't warn about wrong format specifiers. Commented Jul 28, 2015 at 14:09
  • @MichaelWalz if someone can get Turbo C++ running on 64bit linux, they probably know about format specifiers :) Commented Jul 28, 2015 at 14:36

2 Answers 2

3
 char str1[10] = "\0";
 char str2[10] = "\0";

Well range of str1 and str2 is from 0 to 9 , so both can hold only 9 characters and last one null.

strcpy(str1, "168.98.173.44");// storing more than 10 characters
strcpy(str2, "168.97.176.146");// storing more than 10 characters

Plus-

printf("main: str1 at %u is %s\n\n", &str1, str1);
                       ^this specifier is to print unsigned int .

To print address use %p.

Sign up to request clarification or add additional context in comments.

5 Comments

And don't forget the null-terminator.
Thanks ameycu and EOF for the replies. I tried changing the size to 50 and also added strcat(str1, "\0");. But the issue persists.
@RupeshKumar: Well, why don't you fix your scanf()-format strings? %d expects a pointer to an int, you pass a pointer to a short, so format should be %hd.
@RupeshKumar Well I don't any reason it will not give correct option as specifier for short is also pointed by EOF SIR.
Thanks EOF and amey. Correcting the format to "hd" or changing the datatype of variable to int fixes the issue. So in the bug datatype was of short i.e. 2 bytes while I was filling 4 bytes through %d format specifier due to which the result was undefined and it used to segfault.
2

For the question. You should use %p for printing the pointer's address, not %u.

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.