Compiler error :

This is a discussion on Compiler error : within the C Programming forums, part of the General Programming Boards category; Error is this : 1>e:\documents\visual studio 2005\projects\ozumsafaoglu\ozumsafaoglu\ozumsafaog lu.cpp(15) : error C2440: 'initializing' : cannot convert from 'void *' to 'char ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    151

    Compiler error :

    Error is this :


    1>e:\documents\visual studio 2005\projects\ozumsafaoglu\ozumsafaoglu\ozumsafaog lu.cpp(15) : error C2440: 'initializing' : cannot convert from 'void *' to 'char *'
    1> Conversion from 'void*' to pointer to non-'void' requires an explicit cast

    15th line is this :

    char *a=malloc(50*sizeof(char));

    What is the problem about the casting?

  2. #2
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    Also in the same program :

    Code:
    // ozumsafaoglu.cpp : Defines the entry point for the console application.
    //
    
    #include "stdio.h"
    #include "stdafx.h"
    #include "stdlib.h"
    int _tmain(int argc, _TCHAR* argv[])
    {
    	struct section {
    		char first[30];
    		char second[30];
    		int secno;
    	};
    	int totalline=0; // To find the total section number //
    	 char a[40]; // To take each line of the txt to find the total section number //
    	FILE *fp;
    	if((fp=fopen("liste.txt","r"))==NULL) {
    		perror("liste.txt");
    	}
    	else {
    		while((fgets(a,sizeof(a)-1,fp))!=NULL)
    		{
    			totalline+=1;
    		}
            struct section sections[(totalline+1)/3]; // in order to provide minimum memory spent //
    		rewind(fp);
           int i=0; // will be in for loop in order to control line number 
    	   for(i=0;i<totalline;i++) {
    			
    			if( i &#37; 3==0) {
    				sections[i].secno= i/3 + 1;
    				fgets(sections[i/3+1].first,29,fp);
    				fgets(sections[i/3+1].second,29,fp);
    			    fgets(a,sizeof(a)-1,fp);
    			}
    	   }
    
    	   for(i=0;i<totalline;i++) {
    		   printf("section %d\n",sections[i].secno);
    		   printf("%s\n%s\n\n",sections[i].first,sections[i].second);
    	   }
    	
    	
    	
    	}
    
    
    
    
    
    
    
    	
    	return 0;
    }
    Error messages :

    1>------ Build started: Project: ozumsafaoglu, Configuration: Debug Win32 ------
    1>Compiling...
    1>ozumsafaoglu.cpp
    1>e:\documents\visual studio 2005\projects\ozumsafaoglu\ozumsafaoglu\ozumsafaog lu.cpp(17) : warning C4996: 'fopen' was declared deprecated
    1> d:\program files\microsoft visual studio 8\vc\include\stdio.h(234) : see declaration of 'fopen'
    1> Message: 'This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_DEPRECATE. See online help for details.'
    1>e:\documents\visual studio 2005\projects\ozumsafaoglu\ozumsafaoglu\ozumsafaog lu.cpp(26) : error C2057: expected constant expression
    1>e:\documents\visual studio 2005\projects\ozumsafaoglu\ozumsafaoglu\ozumsafaog lu.cpp(26) : error C2466: cannot allocate an array of constant size 0
    1>e:\documents\visual studio 2005\projects\ozumsafaoglu\ozumsafaoglu\ozumsafaog lu.cpp(26) : error C2133: 'sections' : unknown size
    1>Build log was saved at "file://e:\Documents\Visual Studio 2005\Projects\ozumsafaoglu\ozumsafaoglu\Debug\Buil dLog.htm"
    1>ozumsafaoglu - 3 error(s), 1 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========



    -----------------------------
    The aim of my program :

    There is a text file :

    Tuesday 15:40 17:30 F19
    Thursday 15:40 17:30 F17

    Tuesday 15:40 17:30 FZ19
    Thursday 12:40 14:30 F19

    Tuesday 15:40 17:30 F15
    Thursday 12:40 14:30 FZ19

    Monday 10:40 12:30 F19
    Thursday 12:40 14:30 F15

    My program takes each pairs of line in individual sturct variables. That moduler of 3 is used for this aim.Than I will assign section number to each of them and print them on the screen with section numbers. I am trying to achive taking them into struct successfully.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,755
    Yeah, you're using a C++ compiler to compile C code.

    Or you should be using new and not malloc in your C++ code.

    Decide which language you're using, and write the code and choose the compiler accordingly.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    I have made a this kind of change :
    Code:
    FILE *fp;
    	FILE **fpt;
    	
    	fpt=&fp;
    	if((fopen_s(fpt,"liste.txt","r"))==NULL) {
    		perror("liste.txt");
    	}
    Errors became this:
    1>------ Build started: Project: ozumsafaoglu, Configuration: Debug Win32 ------
    1>Compiling...
    1>ozumsafaoglu.cpp
    1>e:\documents\visual studio 2005\projects\ozumsafaoglu\ozumsafaoglu\ozumsafaog lu.cpp(29) : error C2057: expected constant expression
    1>e:\documents\visual studio 2005\projects\ozumsafaoglu\ozumsafaoglu\ozumsafaog lu.cpp(29) : error C2466: cannot allocate an array of constant size 0
    1>e:\documents\visual studio 2005\projects\ozumsafaoglu\ozumsafaoglu\ozumsafaog lu.cpp(29) : error C2133: 'sections' : unknown size
    1>Build log was saved at "file://e:\Documents\Visual Studio 2005\Projects\ozumsafaoglu\ozumsafaoglu\Debug\Buil dLog.htm"
    1>ozumsafaoglu - 3 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


    It is interesting though. So my fopen function is not secure , is it like reaching the file pointer after reaching the other FILE pointer(**) which points to it?

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,755
    *shrug* - I tried, but was ignored.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    No I heard you. But visual c++ is capable of compiling c codes. stdafx contains the stdio library. Doesnt it ?

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,755
    It will compile it as C, if you name your files as prog.c, not prog.cpp.

    > stdafx contains the stdio library. Doesnt it ?
    It may do. Normally, it's empty when the project is created.
    It's only there as a magic cookie to support m$ broken precompiled header rubbish. If you turn off precompiled headers, you can delete all that stdafx stuff as well.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    I simply had a look at the inside of stdafx. There was #include stdio.h . But I added the second include anyway. It compiles any program but this.

  9. #9
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    I have tried a differemnt program. COmpiler did not give error. But program gave failure. I will give you the soruce code , so you can decide this is because of compiler or me.

    Code:
    #include "stdafx.h"
    #include "stdio.h"
    #include "string.h"
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	char *araba[3];
    	int i=0;
    	for(i=0;i<3;i++) araba[i]=NULL;
    	char x[6]="naber";
    	
    	strcpy_s(araba[0],5*sizeof(char),x);
    	printf("%s",araba[0]);
    	return 0;
    }

  10. #10
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    It's because of you.
    Kurt

  11. #11
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    Wait , what the hell is kurt? If you are meaning the race , that is ugly and unrelated with me. If you did not mean this , I am sorry .
    But why did you say because of you and went away. Why didnt you at least try to express it .

  12. #12
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by ozumsafa View Post
    But why did you say because of you and went away. Why didnt you at least try to express it .
    You didn't ask.
    This time it sounds a litle more like a question.
    Code:
    strcpy_s(araba[0],5*sizeof(char),x);
    araba[0] doesn't point anywhere useful.
    Kurt -- That's my name ( not my fault )

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    When using malloc(), you need to #include <stdlib.h> (in C - if you are writing C++, #include <cstdlib> or better yet use new() instead). That should eliminate the casting warning.

  14. #14
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    I am sorry for my post kurt. I rreally forgot to put & there , but isnt tht araba[0] already pointer.?

    (About kurt , I didnt guess you would put your name under your message)

  15. #15
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Shure it's a pointer but you have initialized it to point to null. You cannot copy anything to that location.
    Kurt

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  2. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 02:17 PM
  3. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 09:11 AM
  4. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM

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