| | |
TO CHAINSAW - or anybody else who knows C++
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
Hi C++,
You cannot use a char* pointer for which you have not allocated space. char* support contains a junk value unless you do a) or b).
You can either
a) not use a pointer. do char[255] support;
b) use malloc or new. char* support; support = new char[255];
if you do option b), then you must remember to do delete support; when you are done with it.
You shouldn't be doing *support = "....."
use the strcpy from C.
C++, which certification are you going for? I strongly advise you to take a class in C++. Getting the answers from this forum alone won't help you pass.
Ed
You cannot use a char* pointer for which you have not allocated space. char* support contains a junk value unless you do a) or b).
You can either
a) not use a pointer. do char[255] support;
b) use malloc or new. char* support; support = new char[255];
if you do option b), then you must remember to do delete support; when you are done with it.
You shouldn't be doing *support = "....."
use the strcpy from C.
C++ Syntax (Toggle Plain Text)
#include <strings.h> // for the strcpy.. #define WINDOWS_LINE "888-555-1234 option 2" #define MAC_LINE 888-555-1234 option 3" // .... in your code // REMEMBER to do option a) or b) like I said #ifdef WINDOWS strcpy(support, WINDOWS_LINE); #else strcpy(support, MAC_LINE); #end
C++, which certification are you going for? I strongly advise you to take a class in C++. Getting the answers from this forum alone won't help you pass.
Ed
•
•
•
•
Originally Posted by C++
char*support;
if(WINDOWS)*support="888-555-1234 option 2"; *support="888-555-1234 option 3";
but i'm wrong.
Please help,
C++
I've got through 226 of 234 questions of certification on my own. These last eight are VERY difficult. When I tried to run your code through the compiler, the compiler says I need to use char type and that your code declaresª support twice or acts unpredictably when neither macro is defined. Can you please help me mith this and the other ?s posted on: TO CHAINSAW or anybody else who knows C++.
Thanks,
C++
P.S. Is the correct code for the problem:
#include <strings.h>
#define WINDOWS ("888-555-1234 option 2")
#define MACOS ("888-555-1234 option 3")
char* support;
*support = new char[255];
#ifdef WINDOWS{
strcpy(*support, WINDOWS);
delete[] *support;
}
#else{
strcpy(*support, MACOS);
delete[] *support;
}
#end
Oh yeah... I solved problems #1, 3, and 4.
Thanks,
C++
P.S. Is the correct code for the problem:
#include <strings.h>
#define WINDOWS ("888-555-1234 option 2")
#define MACOS ("888-555-1234 option 3")
char* support;
*support = new char[255];
#ifdef WINDOWS{
strcpy(*support, WINDOWS);
delete[] *support;
}
#else{
strcpy(*support, MACOS);
delete[] *support;
}
#end
Oh yeah... I solved problems #1, 3, and 4.
[QUOTE=C++]I've got through 226 of 234 questions of certification on my own. These last eight are VERY difficult. When I tried to run your code through the compiler, the compiler says I need to use char type and that your code declaresª support twice or acts unpredictably when neither macro is defined. Can you please help me mith this and the other ?s posted on: TO CHAINSAW or anybody else who knows C++.
Thanks,
C++
[QUOTE]
ahhh i see im sorry, i misunderstood you, thought you had only done the first ten and were getting help for each. not that you only had ten left :-p
Thanks,
C++
[QUOTE]
ahhh i see im sorry, i misunderstood you, thought you had only done the first ten and were getting help for each. not that you only had ten left :-p
Dont forget to spread the reputation to those that deserve!
cosi's version of #ifdef WINDOWS looks better than yours; WINDOWS is a pre-processor directive defined outside your code, so you'd say something like
C++ Syntax (Toggle Plain Text)
const char *support = NULL; // const char because we'll point to things we can't change. #ifdef WINDOWS support = "888-555-1234 option 2"; // point to the constant string #endif #ifdef MACOS support = "888-555-1234 option 3"; #endif if (support) printf( "%s\n", support ); // one of the options was taken else printf( "Neither WINDOWS nor MACOS were defined!\n" );
No. Your code is wrong. I thought I taught you how pointers and dereferencing work. *sigh* I guess I won't lose my cool over your fishing for answers. Do heed our advice; All these questions should come easy to those seeking certification. Could you please give a background of your experience with C++? I still think you need to take a class and get some real project experience with the language.
Anyways, I cooked up the solution below. Study it and understand what each line means. You should be able to reproduce the same functionality without any references if you are looking to get certification.
Ed
Anyways, I cooked up the solution below. Study it and understand what each line means. You should be able to reproduce the same functionality without any references if you are looking to get certification.
Ed
C++ Syntax (Toggle Plain Text)
#include <stdio.h> #include <strings.h> #define WINDOWS #define WL "hi" #define ML "hi2" void test() { char *support = new char[255]; #ifdef WINDOWS strcpy(support, WL); #elif MAC strcpy(support, ML); #endif printf("%s", support); delete support; } int main() { test(); }
•
•
•
•
Originally Posted by C++
#include <strings.h>
#define WINDOWS ("888-555-1234 option 2")
#define MACOS ("888-555-1234 option 3")
char* support;
*support = new char[255];
#ifdef WINDOWS{
strcpy(*support, WINDOWS);
delete[] *support;
}
#else{
strcpy(*support, MACOS);
delete[] *support;
}
#end
Oh yeah... I solved problems #1, 3, and 4.
C++, you posed the question as:
"Write conditionalª code that will produce the line of source code charª *support="888-555-1234 option 2"; if the macro WINDOWS is defined, and charª *support="888-555-1234 option 3"; if the macro MACOS is defined. (Note: it will never be the case that both MACOS and WINDOWS are simultaneously defined.) "
so I guess the literal interpretation of that is:
Though I still prefer my suggestion for a couple of reasons; (1) the char* may as well be a const char*, though that's not what the question asked, and (2) what if neither WINDOWS nor MACOS were defined? The question doesn't say, but I think in a real program you'd have to deal with that.
COSI - were you saying my code was wrong? What was wrong with it? You said something about pointers and dereferencing; can you enlighten me?
"Write conditionalª code that will produce the line of source code charª *support="888-555-1234 option 2"; if the macro WINDOWS is defined, and charª *support="888-555-1234 option 3"; if the macro MACOS is defined. (Note: it will never be the case that both MACOS and WINDOWS are simultaneously defined.) "
so I guess the literal interpretation of that is:
C++ Syntax (Toggle Plain Text)
#ifdef WINDOWS char* support = "888-555-1234 option 2"; #endig #ifdef MACOS char* support = "888-555-1234 option 3"; #endif
COSI - were you saying my code was wrong? What was wrong with it? You said something about pointers and dereferencing; can you enlighten me?
Can you help me with the following problem too?
Define a macro MAX that accepts two parametersÂ* and expands to the valueÂ* of the largerÂ* of the two (you'll need the conditionalÂ* operatorÂ* for this).
I think the answer is:
#define MAX(x,y) (
if(x>y)delete(y)
else if((y)>(x))delete(x)
)
Thanks,
C++
Define a macro MAX that accepts two parametersÂ* and expands to the valueÂ* of the largerÂ* of the two (you'll need the conditionalÂ* operatorÂ* for this).
I think the answer is:
#define MAX(x,y) (
if(x>y)delete(y)
else if((y)>(x))delete(x)
)
Thanks,
C++
![]() |
Similar Threads
- free() = delete or delete[]? (C++)
- Forum lurkers, introduce yourself ... !! (Community Introductions)
- Recursive prime number f(x) (C)
- passing objects as arguments (C++)
Other Threads in the C++ Forum
- Previous Thread: ive got a Q
- Next Thread: how to start out
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count data database delete desktop directshow dll download dynamic encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib library linkedlist linker linux loop looping loops map math matrix memory newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive return string strings struct studio temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





