Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old December 24th, 2005, 04:35 PM
Aris
Guest
 
Posts: n/a
Default problem in a very simple code

Hi all!
this is the code:
#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

void MyFunction(char *B[])
{
cout<<sizeof(B)<<endl; //(1)
}

int main()
{
char* B[]={"1","2","we"};
MyFunction(B);
cout<<sizeof(B)<<endl;//(2)

system("PAUSE");
return 0;
}

and this is the problem :
in the (1) cout I get the answer :4
and in the (2) cout I get 12!
How can I handle this please?




  #2  
Old December 24th, 2005, 04:55 PM
Gianni Mariani
Guest
 
Posts: n/a
Default Re: problem in a very simple code

Aris wrote:[color=blue]
> Hi all!
> this is the code:
> #include <iostream.h>
> #include <stdlib.h>
> #include <stdio.h>
> #include <string.h>
>
> void MyFunction(char *B[])
> {
> cout<<sizeof(B)<<endl; //(1)
> }
>
> int main()
> {
> char* B[]={"1","2","we"};
> MyFunction(B);
> cout<<sizeof(B)<<endl;//(2)
>
> system("PAUSE");
> return 0;
> }
>
> and this is the problem :
> in the (1) cout I get the answer :4
> and in the (2) cout I get 12!
> How can I handle this please?[/color]

This should be in the FAQ - but here is a previous thread on the topic.

http://groups.google.com/group/comp....bfe04d1eaf64a8

Options are:

a) pass a length
b) use a template
c) do without


Example of a)
void MyFunction(char *B[], int length )
{
cout<<sizeof(*B)*length<<endl; //(1)
}

Example of b)

template <int N>
void MyFunction(char *(&B)[N])
{
cout<<sizeof(B)<<endl; //(1)
}
  #3  
Old December 24th, 2005, 10:15 PM
BobR
Guest
 
Posts: n/a
Default Re: problem in a very simple code


Aris wrote in message ...[color=blue]
>Hi all!
>this is the code:
>#include <iostream.h>
>#include <stdlib.h>
>#include <stdio.h>
>#include <string.h>
>
>void MyFunction(char *B[]){
>cout<<sizeof(B)<<endl; //(1)
>}
>
>int main(){
>char* B[]={"1","2","we"};
>MyFunction(B);
>cout<<sizeof(B)<<endl;//(2)
>
>system("PAUSE");
>return 0;
>}
>
>and this is the problem :
> in the (1) cout I get the answer :4
>and in the (2) cout I get 12!
>How can I handle this please?[/color]

By using C++?

#include <iostream>
#include <ostream> // for endl.
// #include <cstdlib> // #include <cstdio>
#include <string>
#include <vector>

void MyFunction( std::vector<std::string> const &Bstr ){
std::cout<<"in "<<__FUNCTION__<<" size="<<Bstr.size()
<<std::endl; //(1)
for(size_t i(0); i< Bstr.size(); ++i){
std::cout<<"Bstr.at("<<i<<")"<<Bstr.at(i)<<std::en dl;
}
}

int main(){
std::string B[]={"1","2","we"};
std::vector<std::string> vBstr;
std::copy(B, B+(sizeof B/sizeof *B), std::back_inserter(vBstr));
MyFunction( vBstr );
std::cout<<"string array size = "<<sizeof B/sizeof *B<<endl;//(2)

system("PAUSE");
return 0;
}

--
Bob R
POVrookie


  #4  
Old December 25th, 2005, 04:55 AM
red floyd
Guest
 
Posts: n/a
Default Re: problem in a very simple code

BobR wrote:[color=blue]
> Aris wrote in message ...
>[color=green]
>>[redacted][/color]
>
>
> By using C++?
>[/color]
[redacted][color=blue]
>
> void MyFunction( std::vector<std::string> const &Bstr ){
> std::cout<<"in "<<__FUNCTION__<<" size="<<Bstr.size()
> <<std::endl; //(1)[/color]

If you're going to be pedantic to the OP, then try using Standard C++
yourself. __FUNCTION__ is a g++-ism, and is not part of the Standard.
[color=blue]
> system("PAUSE");
> return 0;
> }
>
> --
> Bob R
> POVrookie
>
>[/color]
  #5  
Old December 25th, 2005, 06:55 AM
BobR
Guest
 
Posts: n/a
Default Re: problem in a very simple code


red floyd wrote in message ...[color=blue]
>BobR wrote:[color=green]
>>
>> void MyFunction( std::vector<std::string> const &Bstr ){
>> std::cout<<"in "<<__FUNCTION__<<" size="<<Bstr.size()
>> <<std::endl; //(1)[/color]
>
>If you're going to be pedantic to the OP, then try using Standard C++
>yourself. __FUNCTION__ is a g++-ism, and is not part of the Standard.[/color]

Yup, I done screwed up big-time. You are right. I am so very sorry. Can you
ever forgive me? Please?

Dang, you mean there are people out there who use something other than GCC?
<G>
[ "__FUNCTION__" and "__PRETTY_FUNCTION__" sure are handy. ]

Merry Christmas and a Happy New Year to all!

--
Bob R
POVrookie


  #6  
Old December 25th, 2005, 05:05 PM
red floyd
Guest
 
Posts: n/a
Default Re: problem in a very simple code

BobR wrote:[color=blue]
> red floyd wrote in message ...[/color]
[color=blue]
> Yup, I done screwed up big-time. You are right. I am so very sorry. Can you
> ever forgive me? Please?
>
> Dang, you mean there are people out there who use something other than GCC?
> <G>
> [ "__FUNCTION__" and "__PRETTY_FUNCTION__" sure are handy. ]
>
> Merry Christmas and a Happy New Year to all!
>[/color]
[Entire post taken in the spirit it was intended]

No problem Bob. Happy Chanukah!
  #7  
Old December 27th, 2005, 07:55 AM
Aris
Guest
 
Posts: n/a
Default for me is difficult!

Hi all!
I want to make a function which will create file names as :
I will load with an integer
from 1 till 5 and receive a responce like this
"file1.txt","file2.txt"....."file5.txt"
But instead I get crazy things like theese:
file1.txt
file1.txt2
file1.txt23
file1.txt233@
can you help me?

#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

char * Create_File_Name(int counter_files)
{
char *ch1="file";
char *ch2=".txt";
char string[2];
itoa(counter_files,string,10);
char *ch3=strcat(ch1,string);
char *fname=strcat(ch3,".txt");
return fname;
}


int main()
{

char* fname;
for(int i=1; i<5; i++)
{
fname=Create_File_Name(i);
cout<<fname<<endl;
}
int p;
cin>>p;
return 0;
}






  #8  
Old December 28th, 2005, 06:45 AM
Luke Meyers
Guest
 
Posts: n/a
Default Re: for me is difficult!

There's unfortunately a great deal going wrong here. Your program
invokes undefined behavior, because the buffers you pass to strcat do
not have sufficient size to hold the concatenated strings. If you're
going to use the old C libraries, you should at least read up on them
to avoid such mistakes:
http://www.cplusplus.com/ref/cstring/strcat.html

That said, it's a bad idea to be using all this legacy, C-style stuff
in any case. Unless you are under some absurd constraint to do so, you
should instead use the STL's string, and associated practices. Here is
an updated, working version of your code:

#include <iostream> // for std::cout
#include <string> // for std::string
#include <sstream> // for std::stringstream

std::string createFileName(int fileIndex) {
std::stringstream stream;
stream << "file" << fileIndex << ".txt";
stream.flush();
return stream.str();
}

int main(void) {
for(int i=1; i<=5; i++) {
std::cout << createFileName(i) << "\n";
}
return 0;
}

Luke

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles