Pointer from integer without cast

This is a discussion on Pointer from integer without cast within the C Programming forums, part of the General Programming Boards category; Hello all, I had a question regarding strings and arrays, and how to properly use strcmp. I have an assignment ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    5

    Pointer from integer without cast

    Hello all,

    I had a question regarding strings and arrays, and how to properly use strcmp.

    I have an assignment where one of the objective is to have the user input a .txt file and to use C to analyze some statistics on it. We have to do error checking on it, so we must check to see if the file the user inputed ended in ".txt". If the file does not end in .txt, an error message must print to the screen saying only .txt files may be analyzed. All of my program is working correctly except for this part:

    Code:
    if (strcmp(".txt",&fn[strlen(fn)-4] != 0))
      {
    	printf("Only files ending with .txt can be analyzed.\n");
    	printf(" Your file: %s\n",fn);
      }
    It should be noted that fn is the filename that has been passed through to this function and is being analyzed. We MUST use strcmp to determine whether or not a file is .txt. As an example:

    Let's say I have a filename of file.txt. In this program, it's stored in C as:

    file.txt\n\0

    The reason the \n character is in there is because we had to use the "fgets" function to retrieve the filename, and it automatically adds the \n. In another function, I removed the \n character, so by the time it gets to the above if statement, the filename will be:

    file.txt\0

    So I need to make my strcmp check to see if the filename ends in ".txt". The index number in the above example is 8. So the first index I would check would be position [4] to see if it is a "."

    Next, I would check position [5] to see if it was a "t" and so on..

    I realize the code I have above is completely wrong because I'm only calling on one part of fn and asking it whether it's equal to ".txt", which will obviously be false. Would I need to use a while or for loop?

    The complier error code is what the subject of the thread is, so any help would be greatly appreciated. Thank you.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    if (strcmp(".txt",&fn[strlen(fn)-4] != 0))
    Code:
    if (strcmp(".txt",&fn[strlen(fn)-4]) != 0)
    ?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,248
    Quote Originally Posted by STDSkillz View Post
    Code:
    if (strcmp(".txt",&fn[strlen(fn)-4] != 0))
      {
    	printf("Only files ending with .txt can be analyzed.\n");
    	printf(" Your file: %s\n",fn);
      }
    Yikes. What if strlen(fn) is less than 4? You end up with a negative result from strlen(fn) - 4, which is cast to size_t, which is unsigned, so it becomes a gigantic positive value, and the universe explodes. EDIT: Sorry, misread the code. You end up with a negative index, not a huge positive index, but it's still totally wrong.

    It should be noted that fn is the filename that has been passed through to this function and is being analyzed.
    You still haven't said what TYPE "fn" is. "Filename" isn't a type.

    Let's say I have a filename of file.txt. In this program, it's stored in C as:

    file.txt\n\0

    The reason the \n character is in there is because we had to use the "fgets" function to retrieve the filename, and it automatically adds the \n.
    fgets() doesn't add anything to anything.
    Last edited by brewbuck; 10-22-2007 at 09:41 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers, structures, and malloc
    By lugnut in forum C Programming
    Replies: 24
    Last Post: 10-09-2008, 04:52 PM
  2. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  3. Problem with "pointer from integer without a cast"
    By firyace in forum C Programming
    Replies: 6
    Last Post: 05-11-2007, 09:48 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 09:43 AM

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