블로그 이미지
Leeway is... the freedom that someone has to take the action they want to or to change their plans.
maetel

Notice

Recent Post

Recent Comment

Recent Trackback

Archive

calendar

1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
  • total
  • today
  • yesterday

Category

Steve Oualline
Practical C Programming

13. Simple Pointers


183p
Pointers are also called address variables because they contain the addresses of other variables.

184p
Pointers can be used as a quick and simple way to access arrays. (...) Pointers can be used to create new variables and complex data structures such as linked lists and trees.

185p
The operator ampersand (&) returns the address of a thing which is a pointer.
The operator asterisk (*) returns the object to which a pointer points.

Operator - Meaning
* - Dereference (given a pointer, get the thing referenced)
& - Address of (given a thing, point to it)

The operator ampersand (&) returns the address of a thing which is a pointer.
The operator asterisk (*) returns the object to which a pointer points.


int *thing_ptr; // declare a pointer to a thing
thing_ptr = &thing; // point to the thing
*thing_ptr = 5; // set "thing" to 5
// The expression &thing is a pointer to a thing. The variable thing is an object
// thing_ptr points to any integer. It may or may not point to the specific variable thing.

The & (address of operator) gets the address of an object (a pointer).
The * (dereference operator) tells C to look at the data pointed to, not hte pointer itself.

http://www.cplusplus.com/doc/tutorial/pointers.html

http://www.cprogramming.com/tutorial/lesson6.html


187p
Several pointers can point to the same thing.

188p
Pointers as Function Arguments

The only result of a function is a single return value.

http://en.wikipedia.org/wiki/Call_by_value#Call_by_value

http://www.cplusplus.com/doc/tutorial/functions2.html

NULL pointer
locale.h
http://www.cplusplus.com/reference/clibrary/clocale/

189p
const Pointers

const char *answer_ptr = "Forty-Two";

char *const name_ptr = "Test";

const char *const title_ptr = "Title";

191p
Pointers and Arrays

(reminding...) 서 교수님:
number[10] 이라고 배열 선언을 하면 컴파일러는 10개의 연속된 데이터를 위한 공간을 확보하고 그 첫번째 공간의 주소를 number 에 넣습니다. 그래서 number 는 number[0] 의 주소를 가지는 것입니다.

number[1] 은 배열에서  number[0] 다음의 값을 가지는데 그 공간의 주소값을 알고 싶으면 &number[1] 이라고 하든지 number+1  이라고 하면 됩니다.

거꾸로, number 에서 시작하여 두번째 즉 number[1] 의 값을 주소로부터 얻고 싶으면 number[1] 이라고  하든지 *(number+1) 로 하여 얻을 수 있습니다.

192p
A pointer can be used to find each element of the array.

197p
Using Pointers to Split a String

strchr()
http://www.cplusplus.com/reference/clibrary/cstring/strchr.html


201p
Pointers and Structures

Instead of having to move a lot of data around, we can declare an array of pointers and then sort the pointers.


Command-Line Arguments

main (int argc, char *argv[])
{
The parameter argc is the number of arguments on the command line (including the program name).
The array argv contains the actual arguments.

(reminding...)
터미널에서, 말씀하신대로 "./V2008122-01 input.txt"라고 치면 다음과 같이 나옵니다.
argc=2
argv[0]=./V2008122-01
argv[1]=input.txt

서 교수님:
argv[0] = 실행프로그램 이름; 1번째는 항상 실행프로그램의 패스/이름 이 들어갑니다.
이번에는 argv[1] 에 들어갈 두 번째 값을 주었기 때문에 그 값이 프린트 된 것입니다.
command-line arguments는 shell 프로그램이 fileio 함수를 호출할 때 매개변수로 주는 것이고, 좀 더 엄밀하게는 OS 가 main 함수를 호출할 때 주는 것입니다.



http://www.cplusplus.com/reference/clibrary/cstdio/fprintf.html

Example 13-12: print.c
// formats files for printing
// usage: print [options] file(s)

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

int verbose = 0; // verbose mode (default = false)
char *out_file = "print.out"; //output filename
char *program_name; // name of the program for erros
int line_max = 66; // number of lines per page

void do_file(char *name)
{
    printf("Verbose %d Lines %d Input %s Output %s\n",
           verbose, line_max, name, out_file);
}

void usage (void)
{
    fprintf(stderr, "Usage is %s [options] [file-list]\n",
            program_name);
    fprintf(stderr, "Options\n");
    fprintf(stderr, "    -v    verbose\n");
    fprintf(stderr, "    -l<number> Number of line\n");
    fprintf(stderr, "    -o<name>    Set output filename\n");
    exit(8);
}

int main (int argc, char *argv[])
{
    program_name =argv[0];
   
    while ( (argc>1) && (argv[1][0] == '-') )
        // argv[1][1] is the actual option character
    {
        switch (argv[1][1]) {
            case 'v':
                verbose = 1;
                break;
               
            case 'o':
                out_file = &argv[1][2];
                break;
           
            case 'l':
                line_max = atoi(&argv[1][2]);
                break;
           
            default:
                fprintf(stderr, "Bad option %s\n", argv[1]);
                usage();
        }
       
        ++argv;
        --argc;
    }
   
    if (argc == 1) {
        do_file("printf.in");
    }
    else {
        while (argc>1)
        {
            do_file(argv[1]);
            ++argv;
            --argc;
        }
    }
   
    return(0);
}

Xcode 실행창:
Verbose 0 Lines 66 Input printf.in Output print.out

terminal:
999:~/cintro/ch13/eg12 lym$ ./ch13eg12
Verbose 0 Lines 66 Input printf.in Output print.out
999:~/cintro/ch13/eg12 lym$ ./ch13eg12 i am tired
Verbose 0 Lines 66 Input i Output print.out
Verbose 0 Lines 66 Input am Output print.out
Verbose 0 Lines 66 Input tired Output print.out
999:~/cintro/ch13/eg12 lym$ ./ch13eg12 -v -l128 -0title xfile yfile zfile
Bad option -0title
Usage is ./ch13eg12 [options] [file-list]
Options
        -v      verbose
        -l<number> Number of line
        -o<name>        Set output filename
999:~/cintro/ch13/eg12 lym$ ./ch13eg12 -v -l128 -otitle xfile yfile zfile
Verbose 1 Lines 128 Input xfile Output title
Verbose 1 Lines 128 Input yfile Output title
Verbose 1 Lines 128 Input zfile Output title


208p
A pointer does not create any new space for data, but just refers to data that is created elsewhere.


http://www.cplusplus.com/doc/tutorial/pointers.html
The identifier of an array is equivalent to the address of its first element, as a pointer is equivalent to the address of the first element that it points to, so in fact they are the same concept.

An array can be considered a constant pointer.

http://www.cprogramming.com/tutorial/lesson6.html
Arrays can act just like pointers.


'@GSMC > 서용덕: DMD Programming' 카테고리의 다른 글

Similarity Transformation  (0) 2008.06.10
hw 9 - ppm 이미지 회전시키기  (0) 2008.05.30
[Steve Oualline] 12. Advanced Types  (0) 2008.05.28
data type 정리  (0) 2008.05.24
[Steve Oualline] 11. Bit Operations  (0) 2008.05.14
posted by maetel