another weird unexplained anomaly!

P

puzzlecracker

Fixed font - Proportional font


string initilization WHY?
All 2 messages in topic - view as tree
puzzlecracker Jan 29, 10:47 pm show options

Newsgroups: comp.lang.c
From: "puzzlecracker" <[email protected]> - Find messages by this
author
Date: 29 Jan 2005 22:47:59 -0800
Local: Sat, Jan 29 2005 10:47 pm
Subject: string initilization WHY?
Reply | Reply to Author | Forward | Print | Individual Message | Show
original | Report Abuse

Why would this work? - and it does! any thoughts?


#include<iostream>

using namespace std;


void init(char **str){ *str="Awesome"; }
main(){

char * str;

init(&str);
cout<<str<<endl;
}
 
I

Ian McCulloch

puzzlecracker said:
Fixed font - Proportional font


string initilization WHY?
All 2 messages in topic - view as tree
puzzlecracker Jan 29, 10:47 pm show options

Newsgroups: comp.lang.c
From: "puzzlecracker" <[email protected]> - Find messages by this
author
Date: 29 Jan 2005 22:47:59 -0800
Local: Sat, Jan 29 2005 10:47 pm
Subject: string initilization WHY?
Reply | Reply to Author | Forward | Print | Individual Message | Show
original | Report Abuse

Why would this work? - and it does! any thoughts?


#include<iostream>

using namespace std;


void init(char **str){ *str="Awesome"; }
main(){

char * str;

init(&str);
cout<<str<<endl;
}

Well, technically it shouldn't work because main() is not a valid prototype
for main. It should be 'int main()'. But that is beside the point here.

Other than that, what is the problem? It is equivalent to

int main()
{
char* str;

str = "Awesome";
cout << str << endl;
}

Why wouldn't it work?

Ian
 
T

Tim Love

Why would this work? - and it does! any thoughts?
I think it would help us reply constructively if you could explain what's so
weird, unexplained or anomalous about the pieces of code you're posting, so
that we could focus our comments accordingly. Is it the "main()" that you
find so weird? What sort of "thoughts" do you want?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top