[Cs3] CS-3 question

Mikhail Nesterenko mikhail at cs.kent.edu
Wed Feb 2 12:21:55 EST 2022


> I had two questions from the slides.
> 1) can you explain what extern is like why do we need to use it. If you can
> give an example?

// file x.hpp
extern int x; // global variable declaration
       	      // can be compiled and may exist in multiple files

// file x.cpp
int x=55;  // global variable definition
    	   // may exist only in one source file of a multi-file program


> 2) what is exactly the difference between c-style and c++ style string is?

// c-style string is a null-terminated (special character '\0')
// character array
char s[] = "hi"; // this line declares an array of three characters
     	     	 // s[0] = 'h', s[1] = 'i', s[3] = '\0'


// All rules of arrays apply to the c-style strings.
// For example
char s2[20];
s2 = s; // this is illegal, arrays are not assignable

// c++ style string is a class that holds characters and has a number
// of functions, including overloaded assignment
// therefore
string s2, s1="hi";
s2 = s1; // this is legal

In modern C++, c-style strings are seldom used. However, program
parameters, which we need for a few labs, are passed as c-style strings 

Here is an example of using them:

     http://antares.cs.kent.edu/~mikhail/classes/cs3/Labs/commandLine.cpp

Thanks,
--
Mikhail


More information about the cs3 mailing list