I'm trying to do something similar to this:

int output_type=1
if (output_type==1)
			char real_out[7]="binary";
		else if (output_type==2)
			char real_out=[6]"octal";
		else if (output_type==3)
			char real_out[8]="decimal";
		else {
			char[20]real_out="This REALLY shouldn't have happened.\n";}
		}

I know the coding is kinda messy but I basically want the variable "real_out" to contain either decimal, octal or binary depending on what the value of output_type is.

I'm a newbie to C, we were learning python last semester so string operations and declaring variables is all new to me. I know a lot about strings already but some technicalities I'm learning as I go :p I wanted to do this in order to make an assignment due on Tuesday feel more "complete".

As soon as the block ( between the {} or single statement in the if/else control flow ) is executed the string disappears.
I suppose you want something like:

#include <string.h> /* for strcpy function */
char real_out[20] = { '\0' }; /* array declared outside the if/elses */
int output_type=1; /* you are missing semicolon */
if (output_type==1)
	strcpy( real_out, "binary" ); /* use of strcpy function */
else if (output_type==2)
	strcpy( real_out, "octal" );
else if (output_type==3)
	strcpy( real_out, "decimal" );
else
	strcpy( real_out, "This REALLY shouldn't have happened.\n" );

char real_out=[6]"octal"; char real_out[6] = "octal";
char[20]real_out="This REALLY shouldn't have happened.\n"; char real_out[20] = "....";
Watch how you define the arrays.

The following can also be done .. it does not involve strcpy() or arrays,
instead it uses a single pointer (real_out_ptr)

char const * real_out_ptr = "This REALLY shouldn't have happened.\n";

int output_type = 1;

if (output_type == 1)
	real_out_ptr= "binary";
else if (output_type == 2)
	real_out_ptr = "octal";
else if (output_type == 3)
	real_out_ptr = "decimal";

The following can also be done .. it does not involve strcpy() or arrays,
instead it uses a single pointer (real_out_ptr)

I would not suggest the use of pointers until array of chars are understood.

char real_out=[6]"octal"; char real_out[6] = "octal";
char[20]real_out="This REALLY shouldn't have happened.\n"; char real_out[20] = "....";
Watch how you define the arrays.

I'm not used to C syntax, the crappy laptop keyboard doesn't help me much either :P

As soon as the block ( between the {} or single statement in the if/else control flow ) is executed the string disappears.

Hmm... That explains a lot of things I couldn't understand so far :)

Also, thanks mitrmkar for a nice alternative. Can anyone tell me the reason we have to use pointers? Why can I code

char x[3]="ab"

? Does a pointer to a string point to the whole array? If so, what does x point to then?

Thanks for all the help, everyone :) Marked as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.