0

I have a simple C program that I can compile via command line on Linux easily enough but want to use Visual Studio to debug the program so that I can more easily see what it's doing but I cannot get it to compile.

I have created a new cpp Win32 console application. Added my .c and .h file but I cannot get it to compile. I get 100+ errors that primarily seem to be red herrings. For instance I have an error saying a variable isn't declared when I can see it declared on the line immediately before. For instance:

int i;
for (i = 0; i < unpacked_length; i++)
{

on the "for" line I get an error: "error C2065: 'i' : undeclared identifier"

So something else is obviously going on. One error that seems legit is this one:

static unsigned char* Base64_Charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

IntelliSense: a value of type "const char *" cannot be used to initialize an entity of type "unsigned char *"

Since this works when compiling via command line I don't know why the intellisense would be giving this error here unless this compiler has different rules than the other one I used via command line. I haven't done any C for about 15 years and don't know if this could be a difference in compiler or if there is something else going on here.

The only includes in the source are:

#include "dtcrypt.h"
#include <string.h>
#include <stdlib.h>

Appreciate any help!

2
  • Can you edit your post to include the smallest complete code that exhibits the problem? Commented Jul 6, 2012 at 19:33
  • I will see what I can do. This code is an encryption program that a 3rd party has written for us so I can't post the program. I will reduce it to a size that repro's the problem if indeed the answer doesn't pop to someone by seeing the details of my situation above. Commented Jul 6, 2012 at 19:34

3 Answers 3

3

Visual Studio 2010 only supports ANSI C (aka C89), it does not support the more modern C99 or C11 language standards. ANSI C requires you to declare all of your variables at the top of a function, you cannot define them in the middle of a function just before they are used. So try rewriting your code like this:

void my_function()
{
    /* Variable declarations go here */
    int i;
    ...

    /* Now teh codez */
    for (i = 0; i < unpacked_length; i++)
    {
        ...
    }
}

For the second error, Intellisense complaining that it can't convert a pointer to const char to a pointer to const unsigned char. String literals have type char[] (array of characters), and char is signed by default. So, you should declare your variable as type const char * instead of const unsigned char *. Or, you can ignore it, since it's not a compiler error, but it is a warning at certain warning levels.

Sign up to request clarification or add additional context in comments.

1 Comment

I would have never figured this out. Thanks so much!
1

Whether char is signed or not is a compiler setting in Visual C++. Change it to unsigned to match the expected GCC environment. Project properties, C/C++, Language.

As for i in the loop, there must be some extra info that you're not showing. The two pasted lines look kosher to me.

That said, I have ample experience porting code between VC++ and GCC - it's hardly ever pretty.

1 Comment

You can also just remove the 'unsigned' from the line. If the compiler defaults 'char*' to 'unsigned', it's redundant, otherwise it's wrong.
0

Instead of messing up with compiler settings typecast it to "unsigned char *"

E.g. : unsigned char* Base64_Charset = (unsigned char *)"ABCDEFG";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.