Thread: Why is my variable changing value?

  1. #1
    Registered User Tigers!'s Avatar
    Join Date
    Jun 2009
    Melbourne, Australia

    Why is my variable changing value?

    Gudday all
    Still trying to finish my 1st large (for me anyway) C program at work. Slwoly getting rid of the bugs and errors. Presently I have a situation where variable is being changing to a non-desired avlue and I can't see how, or why, it is being changed.

    I am replacing hard-coded values ("index.dat") with variables so that I only need make changes in 1 place instead of many. The program works well with the variable until this one spot near the end. The enboldened portion is the code in question. The variable is called index_file. It has the correct value until the code reaches the function called sstrncpy when it changes yet the function does not need or use that variable.
    The attachment shows the change in the variable's value as it progresses through the code. I cannot see why it changes from "index.dat" to "\"?
    Maybe it is staring at me in the face but I certainly am oblivious to it. Perhaps I am referencing the variable incorrectly?
     // a 'safe' strncpy
    char* sstrncpy(char* dst, const char* src, size_t len)
    	printf("The source string is %s\n", src);
        if (!len--)
            return NULL;
        strncpy(dst, src, len);
        dst[len] = 0;
    	printf("inside sstrncpy: - The dest string is %s\n", dst);
    	return dst;
    // a 'safe' strncat
    char* sstrncat(char* dst, char const* app, size_t max )
      ptr = dst;
      if( max != 0 )
    	while( --max && *ptr )
    	while( max-- && *app )
    	*ptr++ = *app++;
    	*ptr = 0;
    return dst;
    int main (int argc, char *argv[])
     FILE *indexFile;
     char index_file[10] = "index.dat"; //files that have errors in them stored in here
     char *tiffUrl="";   //string contains full path incl. file name where TIFF file will end up
     char shorttiffUrl[] ="";
     int tiffLength = 12;
     int 1;
     tiffFile = fopen (move_tiff, "w+"); /* open data file "moveTIFF.dat" */
     indexFile = fopen (index_file, "w+");  /* open data file "index.dat"    */
     /* This portion will add index.dat to moveTIFF.dat. Needs to get path as well */
     i = strlen(tiffUrl) - tiffLength + 1; // the + 1 is to grab the last '\' of the path
     printf("1. index_file after opening is %s\n", index_file);
     printf("shorttiffUrl is %s and tiffUrl is %s and i is %d\n", shorttiffUrl, tiffUrl, i);
     printf("2. index_file after opening (and this is the line before sstrncpy) is %s\n", index_file);
     sstrncpy(shorttiffUrl, tiffUrl, i);
     printf("3. After sstrncpy ----- The dest string is %s\n", shorttiffUrl);
     //printf("After sstrncpy ----- shorttiffUrl is %s and tiffUrl is %s\n", shorttiffUrl, tiffUrl);
     printf("4. index_file before index.dat written to moveTiff.dat is %s\n", index_file);
     sstrncat(shorttiffUrl, index_file, 40);
     printf("After strncat ----- shorttiffUrl is %s\n", shorttiffUrl);
     fprintf(tiffFile, "%s\n", shorttiffUrl);
     /*  */
    Last edited by Tigers!; 09-14-2009 at 11:11 PM. Reason: Typo with file name

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    char *tiffUrl="";
    tiffUrl points to a string that has 1 element ('\0')

    char shorttiffUrl[] ="";
    You allocate an array of char with 1 element.

    Then you try to copy one to another. Well a lot of nasty things can happen. It might work in the beginnng and crash afterwards. You don't give the whole code to be sure but I think that is the problem.
    A possible fix is:
    char *tiffUrl= malloc(100);
    tiffUrl[0] = '\0';
    char shorttiffUrl[100];
    char shorttiffUrl[0] = '\0';

  3. #3
    Registered User Tigers!'s Avatar
    Join Date
    Jun 2009
    Melbourne, Australia
    Thank you for your reply.
    I have made your suggestions and and getting a compliation error.
    error: invalid conversion from `void*' to `char*'
    in the line
    char *tiffUrl= malloc(100);
    What is the difference between char *tiffUrl= malloc(100); and char rptline[22]; ?
    Are not both allocating meory to their variable?

    I have a devil of a time working out when to use pointers, derefencing or neither when using characters or strings?

    Are there any good sites that give a good explanation of such use? (I really need to get some good C books. I wish I had not got rid of the ones I had at uni )

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    A variable is basically a name that stores a value.
    int x;
    Here, x stores an integer value.

    A pointer is a name that stores, as its value, an address (which is just another value).
    int *x;
    Here, x stores as its value, a memory address for an integer.

    If you want to put a value into a non-pointer variable, you just assign it, or change it, whatever.
    If you want to put a memory address into a pointer, you do the same thing.
    If you want to get the value at that memory address (not the value OF the memory address), you dereference.

    That's pretty much all there is to it. Just remember, the value a pointer stores is an address. If you don't want that address, or rather, if you want the value of what's at that address, you dereference. If you want an actual address, you don't.

    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to put a check on an extern variable set as flag
    By rebelsoul in forum C Programming
    Replies: 18
    Last Post: 05-25-2009, 03:13 AM
  2. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  3. Replies: 2
    Last Post: 04-12-2004, 01:37 AM
  4. write Variable and open Variable and get Information
    By cyberbjorn in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2004, 01:30 AM
  5. Changing variable
    By Mithoric in forum Windows Programming
    Replies: 4
    Last Post: 03-30-2004, 09:45 PM