assignment makes pointer from integer

This is a discussion on assignment makes pointer from integer within the C Programming forums, part of the General Programming Boards category; I'm trying to write a function to retrieve a particular field from a mysql database. I'm getting a compiler warning ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    15

    assignment makes pointer from integer

    I'm trying to write a function to retrieve a particular field from a mysql database. I'm getting a compiler warning "assignment makes pointer from integer without a cast"

    I'm not sure if I'm using the right logic to go about this but I thought I'd use a similar format which was suggested here last week ...anyway the code looks like this:

    #include <stdio.h>
    #include <stdlib.h>
    #include "mysql.h"


    int chk_sender(char *Uname, char *Udomain)
    {
    MYSQL_RES *result;
    MYSQL *connection, mysql;
    char sql_str[1024];

    mysql_init(&mysql);
    connection = mysql_real_connect(&mysql, "localhost","root", "", "senders", 0, NULL, 0);

    if (connection == NULL)
    {
    printf("mysql_error(&mysql)\n");
    return 1;
    }
    sprintf(sql_str, "SELECT ap_senders FROM sendtable1WHERE pw_name='%s' and pw_domain='%s';", Uname, Udomain);
    result = mysql_query(connection, sql_str);

    if (result != 0)
    {
    printf(mysql_error(connection));
    return 1;
    }
    //now to call mysql store result to store the value in memory
    result = mysql_store_result(connection);
    printf("Rows: %d\n", mysql_num_rows(result));

    //now to free the result set

    mysql_free_result(result);

    //and then to close the connection

    mysql_close(connection);
    printf("Done\n");
    }

    Any hints on how to correct the compiler warning?

    Thanks,

    Regis

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >"assignment makes pointer from integer without a cast"
    ... means that the right side of the = sign is returning an int, which is trying to be assigned to a pointer variable on the left of the = sign.

    Find the line in question, and study it closely.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    1) printf("mysql_error(&mysql)\n");

    Do you really mean to do this?

    2) result = mysql_query(connection, sql_str);

    Does this actually return a pointer? Or an integer?

    3) if (result != 0)

    Since this is actually a pointer, shouldn't you be treating it like one? "if ( result != NULL )".

    4) result = mysql_store_result(connection);

    You realize that you now have a memory leak, right?

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    15
    Hammer and Quzah God,



    Thanks for the quick response.

    Ok, I guess I understand that in the line:

    [ result = mysql_query(connection, sql_str); ]

    the right side of the = sign is by definition of the mysql_query() function an integer and result is not - hence the error. I'm not sure how to remedy it, but I guess the answer is in one of these freakin mysql or c books I've got here - so I'll figure it out.

    Quzah God,

    Could you explain a bit about the "memory leak?"

    The :

    result = mysql_store_result(connection);

    was per the O'Reilly "Managing and Using MySQL" text - which is not to say that I'm using correctly - I just thought I was. If you've a moment to explain this a bit further I'd appreciate it.

    Thanks again for the feedback,

    Regis

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I think you have a memory leak because of your two calls which assign a value to 'result'.

    Consider the following:

    struct node *n;

    n = makeANewNode( );
    n = makeANewNode( );

    Assume the following:

    'makeANewNode' allocates space and
    returns a pointer to it.

    'n' ends up being the only pointer at
    this node.

    You change what n points at by calling
    'makeANewNode' again. Now what's pointing at the node originally allocated by the first call? Nothing. It's floating around out there in memory some place with nothing pointing at it.

    This is called a memory leak. I assume you have one because of the following:

    Code:
    result = mysql_query(connection, sql_str);
    
    if (result != 0)
    {
        printf(mysql_error(connection));
        return 1;
    }
    //now to call mysql store result to store the value in memory
    result = mysql_store_result(connection);
    The first call, 'mysql_query' returns a pointer (assuming this is right). So now 'result' is pointing at something.

    Next you call 'mysql_store_result', which switches what 'result' points at.

    If like our previous example, the first call actually allocates a node some place, and you're now no longer pointing at that space, then you have a memory leak.

    That's what I was talking about. I don't know those functions personally, so it's possible that you don't have a leak.

    Just so you know though, if you have a pointer pointing at something that is dynamicly allocated, and you make it not point to that space any more, you've just caused a memory leak because there is no way to know where to point again to find that allocated space.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 08-23-2008, 01:16 PM
  2. Ban pointers or references on classes?
    By Elysia in forum C++ Programming
    Replies: 89
    Last Post: 10-30-2007, 03:20 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. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. Replies: 3
    Last Post: 01-14-2002, 11:13 AM

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