0

Well, I have the following code:

int main(int argc, char **argv, char **envp)
{
    const char *usuario= NULL;
    while(*envp)
    {
        char *str = *envp++;
        //if(strcmp(str,"USERNAME")==0)
        if(str[0] == 'U' && str[1] == 'S' && str[2]=='E' && str[3]=='R' && str[4] == 'N')
        {
            usuario = str;
            break;
        }
    }
    if(usuario != NULL)
    {
        printf("Hola, bienvenido al programa %s",usuario);
    }
    return 0;
}

And my question is why can't I declare the variable outside the while and initialize it inside?

char *str;
const char *usuario= NULL;
while(*envp)
{
    *str = *envp++;
    if(`...

The compiler says: warning: assignment makes integer from pointer without a cast

5
  • Aside: you should check the environment string is long enough before testing if(str[0] == 'U' && ... && str[4] == 'N'). It should be at least length 5, or the indexing breaks. Commented Feb 5, 2017 at 20:38
  • Thank you my friend, there are two things I could not understand lol. First, how did you get my code to go into a complete picture? I put the code between these characters "` `" but it did not work out that way. And the other is what do you mean "you should check the environment string is long enough before testing"? Because I'm supposed to be reading about the environment variables, and there's always the variable USERNAME. I did not quite understand what you meant by that. Thanks in advance :) Commented Feb 5, 2017 at 20:56
  • "I'm new to this forum" Stack Overflow isn't a forum, it's a Q&A site: stackoverflow.com/tour Commented Feb 5, 2017 at 21:03
  • Is the same, I quote from wikipedia "Un foro de Internet es un sitio de discusión online asincrónico donde las personas publican mensajes alrededor de un tema, creando de esta forma un hilo de conversación jerarquico (thread en inglés)" :) Commented Feb 5, 2017 at 21:20
  • Btw, *str = *envp++; is not an initialization. It's an assignment. Commented Feb 5, 2017 at 22:24

2 Answers 2

2

warning: assignment makes integer from pointer without a cast

The problem is that you're not assigning to your pointer, you're assigning to the value that the pointer points to:

*str = *envp++;

The * before str causes the pointer to be dereferenced. Instead, you probably want:

str = *envp++;
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much friend. a question, When I said str = * envp ++ in the part of * envp ++ I'm also dereferencing? That is, what you are doing is actually saying Str = * envp And then say envp ++;
Yes, *envp++ dereferences envp, but envp is of type char**, so you need to dereference if you want the char* value that it points to.
2

So the problem with this *str = *envp++; is that you are referencing the str pointer and then assigning a pointer to a char. In other words:

str is a pointer to a char

*envp is a pointer to a char

Thus

str = *envp++

would be a correct statement since both are pointers to a char. But in the provided code you are doing the following:

*str is a char

*envp is a pointer to a char

This means that you are trying to assign a "pointer to a char" to a char. The types do not match.

So fix this by changing your code like this:

char *str;
const char *usuario= NULL;
while(*envp)
{
    str = *envp++;
    if(... `

6 Comments

Thank you very much my friend, to see if I understood, then when I say Char * str = * envp ++, is called redeferencing. And actually the compiler does is declare the variable char * str and then does the assignment Str = * envp ++ and other thing, When I said str = * envp ++ in the part of * envp ++ I'm also dereferencing? That is, what you are doing is actually saying Str = * envp And then say envp ++;
Yes, when you say char *str = *envp++. Then the compiler creates a variable called str. The type of str is then a pointer to a char (char*). After that, the envp is dereferenced (*envp). and then incremented once (*envp++). This value is then assigned to str (not *str). In this context the * in char *str is not the same as the * in *envp++. In the first case, the * means "pointer to" and in the second it means "dereference". Understanding pointers are hard, so read up on a bit more and you'll understand it eventually.
Ooooh, thank you again, the last question and I stop bothering :B, in the code const char * user = NULL; there is also dereferencing right?, In reality it says that the memory address is NULL. Example const char * user; and more below initialize user = NULL; And lastly, is there a C book that you recommend where all this comes from? Thanks in advance.
@EmiliOrtega No that is not dereferencing. I just made a blog post about this on my website specially for you, check it out here: http://codehaven.co.za/blog/blogpost/?bid=9. I apologise for the delay, I went to bed after you sent that previous message. Also, the blog post was written very swiftly, let me know if I made any obvious mistakes.
Don't worry , Thanks for answering :D but i have a question. In the code char *str = *envp++ I think what the program is actually doing is first assigning the value of * envp to stdr and then increasing it once. But from what you tell me, it is increasing and then once increased it is assigned to "stdr", I wait your answer to see if I am correct or you. Thanks :) BTW your blog is really good !
|

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.