Lost Password?


Go Back   CodeCall Programming Forum > Software Development > C and C++

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-30-2007, 08:12 AM
Anirban Chakraborty's Avatar   
Anirban Chakraborty Anirban Chakraborty is offline
Learning Programmer
 
Join Date: Oct 2006
Location: India
Posts: 48
Rep Power: 8
Anirban Chakraborty will become famous soon enough
Default char * and char[] problem

I was supposed to pass a (char * string1) as an argument to a method.
where it (the string) is supposed to be assigned to another (char * string2)...
The compiler gave segmentation fault.

whereas if I tried to pass the (char * string1) as the argument to the method and the char string2[16] the compiler gave out
"incompatible types in assignment of `char*' to `char[16]".

I just don't have any more idea left with me to do what I am supposed to do.
__________________
Anirban Chakraborty
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 11-30-2007, 08:45 AM
v0id's Avatar   
v0id v0id is offline
Retired
 
Join Date: Apr 2007
Location: Denmark
Posts: 2,626
Last Blog:
CherryPy(thon)
Rep Power: 28
v0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of light
Send a message via MSN to v0id
Default

Show us the code, which you think produces the segfault. It could be you're using the pointers wrong. You could also step through your program using a debugger.
__________________
05-03-2007 - 11-13-2008
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 11-30-2007, 08:58 AM
Anirban Chakraborty's Avatar   
Anirban Chakraborty Anirban Chakraborty is offline
Learning Programmer
 
Join Date: Oct 2006
Location: India
Posts: 48
Rep Power: 8
Anirban Chakraborty will become famous soon enough
Default The code that gave segmentation fault

Quote:
typedef const char * String;
// class declaration
private:
String fileName;
public:
void bas_setfn(String Filename){
fileName = Filename;
}else; // Throw an error class that will put up an alert on the screen "FileName Should be less than 15 chars"
return;
}
The actual code is a bit too long.
is there any way to pass a char array of finite length as a method argument.
__________________
Anirban Chakraborty

Last edited by Anirban Chakraborty; 11-30-2007 at 09:05 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 11-30-2007, 09:10 AM
v0id's Avatar   
v0id v0id is offline
Retired
 
Join Date: Apr 2007
Location: Denmark
Posts: 2,626
Last Blog:
CherryPy(thon)
Rep Power: 28
v0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of light
Send a message via MSN to v0id
Default

I tried putting what you did show me into a class, and it compiles and runs just fine, without any errors.
Code:
#include <iostream>

typedef const char * String;

class Example
{
        private:
                String filename;
        public:
                void bas_setfn(String filename)
                {
                        this->filename = filename;
                }
};

int main()
{
        Example obj;
        String str = "Hello, World!";

        obj.bas_setfn(str);

        return 0;
}
__________________
05-03-2007 - 11-13-2008
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 11-30-2007, 09:15 AM
Anirban Chakraborty's Avatar   
Anirban Chakraborty Anirban Chakraborty is offline
Learning Programmer
 
Join Date: Oct 2006
Location: India
Posts: 48
Rep Power: 8
Anirban Chakraborty will become famous soon enough
Default

It did...
Thanks a lot...
__________________
Anirban Chakraborty
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 11-30-2007, 09:32 AM
Anirban Chakraborty's Avatar   
Anirban Chakraborty Anirban Chakraborty is offline
Learning Programmer
 
Join Date: Oct 2006
Location: India
Posts: 48
Rep Power: 8
Anirban Chakraborty will become famous soon enough
Default Thanks

I tried it with the constructor too..... It didn't work
So I did this one...
Quote:
for(int i=0; i < 15 && Filename[i] != '\0'; i++)
fileName[i] = Filename[i];
fileName[i] = '\0';
__________________
Anirban Chakraborty
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem combining c++ and .cgi yuide C and C++ 2 04-21-2007 04:06 AM
just made! Deathcry C and C++ 2 03-11-2007 02:27 PM
Calculator program in C roger C and C++ 4 07-04-2006 03:24 PM


All times are GMT -5. The time now is 07:31 AM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 97%

Ads