Thread: Got Error in Pointer increment. Help !!!

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    4

    Got Error in Pointer increment. Help !!!

    Hey guys.. I am facing a problem when I try to compile these codes.. I am practicing on the Pointer increment.. Error I got was "error C2440: '=' : cannot convert from 'void *' to 'int' There is no context in which this conversion is possible"
    Any idea what I did wrongly ? Thanks !!

    Code:
    #include <stdio.h>
    #include <malloc.h>
    #include <stdlib.h>
    int main() {
        int *ptr, *tmp;
    
        ptr = malloc(10 * sizeof(int));
        tmp = ptr;
        int i;
        
        for (i = 0; i < 10; i++) {
            *ptr++ = 65 + i;
        }
    
        printf("List of integers : ");
        for (i = 0; i < 10; i++) {
            printf("%d ", tmp[i]);
        }
        printf("\n\n");
        
        printf("Reversed list of integers : ");
        for (i = 0; i < 10; i++) {
            printf("%d ", *--ptr);
        }
        printf("\n\n");
    
        ptr = tmp++;
        printf("The offset for pointer of integer when it is incremented : %d\n\n\n", tmp - ptr);
        return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    I didn't see any problem at all.
    Which line is it?
    Did you save before compiling?
    What compiler?
    Edit: malloc.h is non-standard
    Last edited by Bayint Naung; 03-19-2011 at 12:31 AM.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    4
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main() {
        int *x = malloc(sizeof (int));
        x[0] = 0;
        int i = 1;
        while (1) {
            if (i > 3)
                break;
            x = realloc(x, i * sizeof (int));
            x[i] = 2 * i++;
        }
        for (i = 0; i < 3; i++) {
            printf("%d ", x[i]);
        }
        return 0;
    }
    Line 5 got error. Saying that error C2440: '=' : cannot convert from 'void *' to 'int'

    I did save adi.. no idea whats wrong..

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Make sure you are compiling as C program!
    Is it the only error? How about realloc() line??

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    stdlib.h includes malloc() so delete that line - it probably should have been <alloc.h>, which you do NOT need here -- you already have malloc() in stdlib.h

    Yes, there is an echo on this post!

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    4
    The output is

    1>------ Build started: Project: Program_executes, Configuration: Debug Win32 ------
    1>Compiling...
    1>Test2_Q1.cpp
    1>c:\users\kokharng\documents\visual studio 2008\projects\program_executes\program_executes\te st2_q1.cpp(5) : error C2440: 'initializing' : cannot convert from 'void *' to 'int *'
    1> Conversion from 'void*' to pointer to non-'void' requires an explicit cast
    1>c:\users\kokharng\documents\visual studio 2008\projects\program_executes\program_executes\te st2_q1.cpp(11) : error C2440: '=' : cannot convert from 'void *' to 'int *'
    1> Conversion from 'void*' to pointer to non-'void' requires an explicit cast
    1>Build log was saved at "file://c:\Users\KokHarng\Documents\Visual Studio 2008\Projects\Program_executes\Program_executes\De bug\BuildLog.htm"
    1>Program_executes - 2 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    1>Test2_Q1.cpp

    Wrong compiler - change your filename extension to dot c, or change your compiler options that are being used in your IDE.

    It's not just int main(). It's either int main(void), or int main(int argc, char *argv[])

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    4
    I am currently using Visual Studio 2008. Even I changed to .c extension still I got errors.

    1>abc.c
    1>c:\users\kokharng\documents\visual studio 2008\projects\testing\testing\abc.c(7) : error C2143: syntax error : missing ';' before 'type'
    1>c:\users\kokharng\documents\visual studio 2008\projects\testing\testing\abc.c(9) : error C2065: 'i' : undeclared identifier

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    But those errors are nothing to do with whether it's C or C++ code.

    Those are syntax errors in your code.

    > x[0] = 0;
    > int i = 1;
    Specifically, C does NOT allow you to mix declarations and statements.
    Swap these two lines over.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Only C99 only allows you to mix declarations and statements.
    VC++ AFIAK is not C99 compliant.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  2. pointer increment error
    By WDT in forum C++ Programming
    Replies: 7
    Last Post: 12-09-2007, 08:01 AM
  3. scope of a pointer?
    By Syneris in forum C++ Programming
    Replies: 6
    Last Post: 12-29-2005, 09:40 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM