Jump to content

char * problem

- - - - -

  • Please log in to reply
12 replies to this topic

#1
freiza

freiza

    Learning Programmer

  • Members
  • PipPipPip
  • 54 posts

        char * var="string";

	var[1]='3';

	cout<<var;


I don't get any compiler error . But the program terminates. Why?

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
That's not the entire program, so there's no way to know.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
freiza

freiza

    Learning Programmer

  • Members
  • PipPipPip
  • 54 posts
#include<iostream>

#include<conio.h>

#include<stdio.h>

using namespace std;

int main()

{

	char * var = "var";

	var[1]='3';

	cout<<var;


    _getch();


    return 0;

}



#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
What you're trying to do is treat var like a dynamically allocated array of char. The problem is, you actually pointed it to a string constant, which CANNOT be changed. var[1]='3' is illegal.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
freiza

freiza

    Learning Programmer

  • Members
  • PipPipPip
  • 54 posts
then what is the use of
const char * var ="var";

#6
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
  • Programming Language:Java, C#, PHP
  • Learning:C, C++, C#, PHP, Transact-SQL, Assembly, Scheme
I believe that means you can change the pointer var but you cannot change *var.

You can do

const char* var = "var";
var = "abc";

not

const char* var = "var";
var[0] = 'a';

When I do this in Code::Blocks my compiler gives this error:"error: assignment of read-only location '*var'|".

Edited by chili5, 20 February 2012 - 05:41 AM.


#7
voral

voral

    Learning Programmer

  • Members
  • PipPipPip
  • 30 posts
Yes. "var" is string constant. And var is pointer on constant string in this case. Use (for training):

char var[SIZE] = "var";

var[1]='f';

where SIZE is size of "var" + 1 or more. But this is true only for the character 1 byte. One character may take to 6bytes in UTF.
The best way is dynamic char array. IMHO

#8
freiza

freiza

    Learning Programmer

  • Members
  • PipPipPip
  • 54 posts
Please tell me the difference between

char * p ="var";
and
const char * p ="var";

I am unable to find any difference.
I cannot modify the string "var" in both the cases then what the difference is?

#9
voral

voral

    Learning Programmer

  • Members
  • PipPipPip
  • 30 posts
This is the same.
Another variant exists: char * const p ="var";
Example:

char *var1 = "test1"; // Compiler has warning

char *var2 = "test2"; // Compiler has warning

const char *var3 = "test3"; // Compiler has warning

char * const var4 = "test4"; // Compiler has warning

char var5[6] = "test5"; // Ok


var2 = var1; // Ok

var3 = var1; // Ok

var4 = var1; // Error


var2[1] = 'z'; // error

var3[1] = 'z'; // error

var4[1] = 'z'; // error

var5[1] = 'z'; // Ok




#10
freiza

freiza

    Learning Programmer

  • Members
  • PipPipPip
  • 54 posts
You want to say that
const char * and char * are same thing?

#11
voral

voral

    Learning Programmer

  • Members
  • PipPipPip
  • 30 posts
But:
	char *v1;

	const char *v2;

	const char *v3;

	v1 = new char[10];

	v1[1] = 't'; // Ok

	v2 = new char[10];

	v2[1] = 't';  // Error

	v3 = v1;

	v3[1] = 't'; //Error



---------- Post added at 12:33 PM ---------- Previous post was at 12:25 PM ----------

freiza said:

You want to say that
const char * and char * are same thing?

Only in one case:

char *var ="conststring";

and

const char *var ="conststring";

It Line is sample very bad code for beginner. Because it is difficult to understand what's going on in this line

#12
freiza

freiza

    Learning Programmer

  • Members
  • PipPipPip
  • 54 posts
Thank You very much.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users