Come determinare la visibilità di "oggetti" come definito nel linguaggio di programmazione C?

0

Questa domanda è stata posta dal mio professore in un esame e non sono riuscito a trovare una risposta su Google, quindi eccomi qui.

Non capisco cosa siano gli oggetti in C.

    
posta Alena 16.09.2016 - 01:55
fonte

2 risposte

6

C ha oggetti, solo non nel senso OO di un oggetto. Fondamentalmente, un oggetto in C è qualcosa che occupa memoria :

3.15
1     object region of data storage in the execution environment, the contents of which can represent values

2     NOTE When referenced, an object may be interpreted as having a particular type; see 6.3.2.1.

La visibilità si riferisce più agli identificatori rispetto alle istanze di oggetto:

6.2.1 Scopes of identifiers
...
2     For each different entity that an identifier designates, the identifier is visible (i.e., can be used) only within a region of program text called its scope. Different entities designated by the same identifier either have different scopes, or are in different name spaces. There are four kinds of scopes: function, file, block, and function prototype. (A function prototype is a declaration of a function that declares the types of its parameters.)
...
4     Every other identifier has scope determined by the placement of its declaration (in a declarator or type specifier). If the declarator or type specifier that declares the identifier appears outside of any block or list of parameters, the identifier has file scope, which terminates at the end of the translation unit. If the declarator or type specifier that declares the identifier appears inside a block or within the list of parameter declarations in a function definition, the identifier has block scope, which terminates at the end of the associated block. If the declarator or type specifier that declares the identifier appears within the list of parameter declarations in a function prototype (not part of a function definition), the identifier has function prototype scope, which terminates at the end of the function declarator. If an identifier designates two different entities in the same name space, the scopes might overlap. If so, the scope of one entity (the inner scope) will end strictly before the scope of the other entity (the outer scope). Within the inner scope, the identifier designates the entity declared in the inner scope; the entity declared in the outer scope is hidden (and not visible) within the inner scope.

Esempi:

int foo; // file scope; visible over entire translation unit.  By default,
         // the name will be exported (visible to) other translation units.

void bar( int bletch ) // bletch has block scope
{
  int blurga;          // as does blurga; neither are visible
  ...                  // outside of bar
  if ( condition() )
  {
    char blurga[N];    // this version of blurga also has block scope,
    ...                // and "shadows" or "hides" the instance declared
                       // outside of the scope of the if statement, and is
                       // not visible outside of the if statement
  }
}

Gli identificatori foo e bar sono visibili ad altre unità di traduzione; per evitare ciò, li dichiareresti con la parola chiave static .

static int foo;

static void bar( ... )
    
risposta data 16.09.2016 - 03:26
fonte
0

"Oggetti" è probabilmente un termine troppo stringa per "C", ma sostituirlo con "Stuff" e abbiamo qualcosa su cui lavorare.

Al suo livello più semplice (nessun gioco di parole previsto) tutto è "visibile" dopo essere stato dichiarato .

All'interno di un file sorgente singolo , ciò significa elencare i prototipi di funzioni nella parte superiore del file. Qualsiasi codice dopo di ciò "conosce" l'aspetto di tale funzione e può compilarlo.

int doSomething( char* name, char* DoB, char* petName ); 

int rc = doSomething( myName, myDoB, "" ); 

Se si omette questa dichiarazione, il compilatore può "assumere" una "forma" standard della dichiarazione di funzione, qualcosa come "int functionName ();". Il tuo codice non verrà compilato perché la firma che hai inteso non corrisponde a quella [erroneamente] presunta.

Tra tutti i file sorgente multipli , spostate quella dichiarazione in un file di intestazione e includetela in qualsiasi file sorgente che deve utilizzare la funzione.

[file.h]
int doSomething( char* name, char* DoB, char* petName ); 

[file.c]
#include file.h; 

int rc = doSomething( myName, myDoB, "" ); 

[someOtherFile.c]
#include file.h; 

int rc = doSomething( yourName, yourDoB, yourPetName ); 
    
risposta data 16.09.2016 - 13:22
fonte

Leggi altre domande sui tag