Thread: pointers and variables

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    4

    pointers and variables

    Hi,

    Apologies if this is a slightly dumb question, I'm desperately trying to refresh my brain on C...

    I want to save the value of argv[1] (from my command line arguments - it's a filename) into a variable so I can use it in a printf statement later in the program.

    Can anyone tell me how?!

    Thanks,
    Emily

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    There are several options, which depend a little bit on what you want to do. Do you mean "save it in a global variable"? If so, you can just do "char *myglobal;" and then "myglobal = argv[x]" somewhere inside main.

    Note that this (ab)uses the fact that main is never going away until you exit your application, and thus local variables used in main are going to remain available until that time too.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    4
    I want to refer to the filename from inside a function rather than in main ... does that change things?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by sailor_girl View Post
    I want to refer to the filename from inside a function rather than in main ... does that change things?
    Not really - if you want to MODIFY the content, it would possibly be a different story.

    But generally, I would do something like this:
    Code:
    void somefunction(char *filename)
    {
        ... 
        // Uses filename ... 
        ... 
    }
    
    int main(int argc, char **argv)
    {
        ...
        somefunction(argv[1]);
        ...
    }
    This avoids making any copy (other than the implicit one where we pass the pointer to the function itself).

    --
    Mats
    Last edited by matsp; 01-08-2009 at 08:34 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    4
    Oh no, now I've completely confused myself. Sorry. Can you explain it again to me quickly?!!

    Basically main takes a filename as its argument and then from main I call a function which saves the contents of the file into a linked list (that bit's all fine though, I think).

    But at the end of my linked list function I need to produce an summary output file which says the results of my linked list actions (word count, time taken etc) and I wanted to include in the output file the name of the original file that was analysed. (sorry, thinking aloud ...)

    So I would put "char *myglobal;" in main and then "myglobal = argv[x]" underneath to save the filename into the myglobal variable, and then in my function I could refer to the filename via the variable "myglobal"? For example by putting "printf("The filename is %s\n", myglobal);" in my function?

    Sorry ... I normally just play with PHP so this is all really different for me ...

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by sailor_girl View Post
    So I would put "char *myglobal;" in main and then "myglobal = argv[x]" underneath to save the filename into the myglobal variable, and then in my function I could refer to the filename via the variable "myglobal"? For example by putting "printf("The filename is %s\n", myglobal);" in my function?
    Yes, but I would still prefer to not store the filename in any variable, just pass along the filename in argv[x] to the "produce results" function - there is no need to put it in a global anywhere.

    If this doesn't make sense to you, perhaps posting your main() function would be beneficial.

    I'm not sure what is different here from PHP - it uses a fairly similar concept of variable scope and parameters - although I know C and C++ far better than I do PHP, I still think the concepts for variables are similar [aside from the multi-type way that PHP allows any variable to be used as an integer, string or floating point variable seamlessly].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    4
    Ah I just can't get to grips with the idea of pointers ... but anyway!! It works!! Amazing, thank you ...

    While I'm here, by the way ...

    Code:
    void function_name(FILE *file) {
    	clock_t start, end;
    	double dif;
    
    [other assignments]
    	
    	start = clock();	// we take a time reading from the start of the function
    
    	do {
    		/* if/else, do/while loops etc ... going through my linked list, searching and inserting */
    	
    	while(pRec != NULL) { /* more code */
    	}
    	
    	end = clock();
    	dif = ( double ) (end - start) / CLOCKS_PER_SEC;	// time to run the linked list function
    	
    	printf ("Reading into a list took %.3lf seconds.\n", dif);
    Is this valid code to time how long the function takes to run? Because I'm getting values which are about half what I was expecting them to be ...

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    1) printf wants %f format, nor %lf for doubles.
    2) What is so difficult about pointers? To put it simply, every variable stores its data somewhere in memory, so it must have a memory address. You can store these memory addresses in pointers. And then work with them from there.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The variability of pointers of variables :)
    By Hawkin in forum C Programming
    Replies: 3
    Last Post: 11-15-2007, 03:49 AM
  2. Use global variables or pointers?
    By RealityFusion in forum C++ Programming
    Replies: 5
    Last Post: 09-22-2005, 08:47 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. Passing variables by pointers...what am I doing wrong?
    By Shadow12345 in forum C++ Programming
    Replies: 2
    Last Post: 06-07-2002, 02:10 PM
  5. pointers and variables
    By dharh in forum C Programming
    Replies: 4
    Last Post: 02-05-2002, 12:53 PM