Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

C Pointers and String constants

Avatar of Pra Sys
Pra SysFlag for India asked on
CC++
20 Comments1 Solution300 ViewsLast Modified:
Hi,

Very basic question as I probably forgot how this works. I was testing rudimentary strcat function from K&R and I found different behavior while giving input in different manner -

#include <iostream>

using namespace std;

void strCat(char *s, char *t) {
    while(*s)
        s++;
    while((*s++ = *t++))
        ;
}

int main() {
    char p[] = "Hello ";
1->    char *s = "Hello ";                 //DO NOT WORK
2->    //s = p;                                   //WORKS
3->    //char s[] = "Hello ";              //WORKS
    char *t = "world!";
    strCat(s,t);

    cout << s << endl;
    return 0;
}

When I try to send string s as given on line by "1->", I get runtime exception. Otherwise if i try as per given on lines "2->" or"3->", it works!!!

Can anybody tell me why "1->" doesn't work??
ASKER CERTIFIED SOLUTION
Avatar of historychef
historychefFlag of United States of America image

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Commented:
This problem has been solved!
Unlock 1 Answer and 20 Comments.
See Answers