env question?

This is a discussion on env question? within the C Programming forums, part of the General Programming Boards category; Code: #include<stdio.h> int main(int argc, char *argv[], char *env[]) { int i; char *set_ABC; for(i = 0; env[i] != NULL; ...

  1. #1
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    env question?

    Code:
    #include<stdio.h>
    
    int main(int argc, char *argv[], char *env[])
    {
    	int i;
    	char *set_ABC;
    
    	for(i = 0; env[i] != NULL; ++i)
    		printf("%s\n", env[i]);
    	set_ABC ="Try me!";
    	return 0;
    	}
    i used this program to list env's but my question was if set ABC="Try me!" was entered would it be among the others or at the end?
    Whats this...
    c:\program files\microsoft visual studio\myprojects\ch14_ex1\ex1.cpp(10) : error C2440: '=' : cannot convert from 'char [8]' to 'char'
    Last edited by correlcj; 08-05-2002 at 06:19 PM.

  2. #2
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    your first bit of code prints all the enviornment vars. nothing i can think of wrong with it.

    your other piece of code however is totally unrelated to environment vars. try allocating space for set_ABC first rather than using it unitizalized and then using strcpy () to put "try me!" in it. but what does that have to do with enviornment vars?
    hello, internet!

  3. #3
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    Code:
    set_ABC ="Try me!";
    You can only do this during initialization. Otherwise you would need to use strcpy()

  4. #4
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    Wink try me!" in it. but what does that have to do with enviornment vars?

    I donno, the book i bought to study this asks only whether this is listed among env aplhabetically or at the end? So far my success has been a failure but i will try what you two said.
    Thanks!
    cj.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >You can only do this during initialization.
    No, this is a valid operation. The pointer is being made to point at a string literal.

    The problem is the last brace, it shouldn't be there and the compiler is complaining.

    -Prelude
    My best code is written with the delete key.

  6. #6
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    ways to define a string in C:
    Code:
    char foobar[] = "Hello";
    
    char foobar[6] = "Hello"; // note 6 not 5, last is for null terminator
    
    char *foobar = "Hello"; // set at initialization - is ok
    
    char *foobar;
    foobar = (char *) malloc (6); // allocate space
    strcpy (foobar, "Hello"); // after initialization you must use string functions to change strings
    free (foobar); // anything malloc()ed must be free()d after you're done with it
    the first and second examples above can also have strcpy() used on them, but not outside of the space you have to work with! otherwise you'll be reaching into memory that's not yours.
    the third example above should not be modified after initalization (thanks hammer).
    Code:
    char *foobar;
    foobar = (char *) malloc (strlen ("Hello") + 1); // strlen returns the size of the inputted string
    strcpy (foobar, "Hello");
    free (foobar); // always free after your done using it, never free before
    god i hope that's all right (it looks right to me) otherwise i'm gonna feel sofa king dumb
    Last edited by moi; 08-05-2002 at 06:26 PM.
    hello, internet!

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
    1- char foobar[] = "Hello";
    2- char foobar[6] = "Hello"; // note 6 not 5, last is for null terminator
    3- char *foobar = "Hello"; // set at initialization - is ok
    the first three examples can also have strcpy() used on them after initilization.
    One thing... you cannot do this on option 3 (above)
    >char *foobar = "Hello";
    >strcpy(foobar, "blurb");
    This is because the pointer is pointing to a string literal which cannot be safely modified.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  2. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. Question about linked lists.
    By cheeisme123 in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2003, 12:36 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21