Thread: Error: assignment makes pointer from integer without a cast

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    2

    Error: assignment makes pointer from integer without a cast

    the following program returns error code:

    distinct.c:18: error: assignment makes pointer from integer without a cast.

    Code:
      #include <stdio.h>#include <stdlib.h>
    
    
    #define MAX_LENGTH 128
    #define MAX_LINES 1024
    
    
    int main(int argc, char *argv[]) {
        int i, j;
        int countFgets = 0;
        int count = 0;
        int numberofDistinctLines = atoi(argv[1]);
        char* string[MAX_LINES];
        for(i = 0; (i < MAX_LINES) && fgets(string[i], MAX_LENGTH, stdin) != NULL; ++i) {
            ++countFgets;
            j = 0;
            count = 0;
            for (j = 0; string[j] != '\0'; ++j){
                if ((string[i] =! string[j])){
                    ++count;
                    if (count == numberofDistinctLines){
                        printf("%c distinct lines seen after %d lines read.\n", numberofDistinctLines, countFgets);
                        return 0;
                    }
                }
            }
        }
        return 0;
    }
    Please help!!!

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    On line 19, you are comparing two strings with the operator !=. Wrong. You should use strcmp.
    Welcome to the forum!
    Last edited by std10093; 05-11-2013 at 07:25 AM.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by std10093
    On line 19, you are comparing two strings with the operator !=
    Or rather, that was probably what you were trying to do. In reality, you wrote this:
    Code:
    if ((string[i] =! string[j])){
    which can be written as:
    Code:
    if (string[i] = !string[j]){
    which is equivalent to:
    Code:
    string[i] = !string[j];
    if (string[i]){
    The result of !string[j] is an int, either 0 or 1. You then convert this int to a pointer, without a cast, in order to assign it to string[i].

    Also, this is not good practice:
    Code:
    for (j = 0; string[j] != '\0'; ++j){
    string is an array of pointers to char. Therefore, string[j] is a pointer to char. You compared string[j] to '\0', but '\0' is a character literal (i.e., of type int). If you want to check if string[j] is not a null pointer, either write:
    Code:
    for (j = 0; string[j]; ++j){
    or more explicitly:
    Code:
    for (j = 0; string[j] != NULL; ++j){
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    May 2013
    Posts
    2
    Thanks for the response. But i am still new to C and you solution didnt work (i fixed the NULL) but i don't know the format for strcmp or the second solution. Could you give a little more detail? For more information i have to created a C program that:

    A C program that when given a single argument n reads lines from standard input until n different lines

  5. #5
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    i don't know the format for strcmp
    It is part of your job to find these things out - and you dont need to ask questions in forums to do that - there are masses of documentation you can easily find
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 17
    Last Post: 06-07-2012, 08:03 PM
  2. Assignment makes pointer from integer without a cast
    By deciel in forum C Programming
    Replies: 4
    Last Post: 12-13-2011, 04:29 PM
  3. assignment makes pointer from integer without a cast
    By mant1s in forum C Programming
    Replies: 4
    Last Post: 10-07-2010, 01:25 AM
  4. Replies: 17
    Last Post: 02-13-2006, 01:19 PM
  5. assignment makes pointer from integer without a cast
    By lithium in forum C Programming
    Replies: 1
    Last Post: 04-02-2003, 11:37 PM