Sponsored Content
Top Forums Programming Pointer arithmetic for list of strings Post 302812107 by Don Cragun on Saturday 25th of May 2013 04:20:08 AM
Old 05-25-2013
Here is a modified version of your code with a few changes and lots of comments added. Please read through the code, examine the output, and let me know if something is still confusing:
Code:
#include <stdio.h>

int main(void)
{       /* Note that list is terminated by a NULL pointer. */
        char *list[] = {"foo", "bar", "baz", NULL};
        char **pl;
        int i;
        /* The common way of setting the number of elements in an array is
        ** to divide the size of the array by the size of the 1st element
        ** in the array.
        */
        int len = sizeof(list)/sizeof(list[0]);

        /* When using 64 bit pointers, each pointer is 8 bytes long. */
        printf("sizeof(list) = %d, sizeof(list[0]) = %d, len = %d\n",
                (int)sizeof(list), (int)sizeof(list[0]), len);

        printf("\nPrinting list using *pl while i < len:\n");
        /* Set pl to the address of the first element in the array list.
        ** Note that in C "list" and "&list[0]" are synonyms.
        */
        for (pl = list, i = 0; i < len; i++, pl++) {
                /* Print the pointer to the pointer. */
                printf("pl = %p\t", pl);        /* &list[i] */
                /* Print the pointed to pointer. */
                printf("*pl = %p\t", *pl);      /* list[i]  */
                /* Print the string pointed to by the pointer to the pointer. */
                printf("%s\n", *pl ? *pl : "NULL pointer");
        }

        printf("\nPrinting list using *pl while *pl != NULL:\n");
        /* If list[]'s last element is not a NULL pointer, this loop will
        ** use random pointers following the array and try to interpret them
        ** as pointers to character strings.  If one of those random values
        ** points outside the address space of the process, we'll get a memory
        ** fault.
        */
        for(pl = list; *pl; printf("%s\n", *pl++));

        printf("\nPrinting list using list[i]:\n");
        for (i = 0; i < len; i++) {
                printf("list[%d] = %p\t", i, list[i]);
                printf("%s\n", list[i] ? list[i] : "NULL pointer");
        }

        return 0;
}

When compiled as a 64-bit application and run on OS X this produces:
Code:
sizeof(list) = 32, sizeof(list[0]) = 8, len = 4

Printing list using *pl while i < len:
pl = 0x7fff6de4db08	*pl = 0x10e24ee70	foo
pl = 0x7fff6de4db10	*pl = 0x10e24ee74	bar
pl = 0x7fff6de4db18	*pl = 0x10e24ee78	baz
pl = 0x7fff6de4db20	*pl = 0x0	NULL pointer

Printing list using *pl while *pl != NULL:
foo
bar
baz

Printing list using list[i]:
list[0] = 0x10e24ee70	foo
list[1] = 0x10e24ee74	bar
list[2] = 0x10e24ee78	baz
list[3] = 0x0	NULL pointer

These 2 Users Gave Thanks to Don Cragun For This Post:
 

10 More Discussions You Might Find Interesting

1. Programming

pass a pointer-to-pointer, or return a pointer?

If one wants to get a start address of a array or a string or a block of memory via a function, there are at least two methods to achieve it: (1) one is to pass a pointer-to-pointer parameter, like: int my_malloc(int size, char **pmem) { *pmem=(char *)malloc(size); if(*pmem==NULL)... (11 Replies)
Discussion started by: aaronwong
11 Replies

2. Programming

pointer arithmetic vs. strlen() & strnlen()?

I have been getting some flack recently for my use of strlen() and strnlen(). Honestly I have always just taken their functionality for granted as being the easiest way of getting the length of a string. Is it really so much better to do pointer arithmetic? What am I gaining besides more... (3 Replies)
Discussion started by: jjinno
3 Replies

3. Shell Programming and Scripting

ksh-script "arithmetic syntax error" comparing strings

Hi all, Iīve already searched the forum but canīt find what i am doing wrong. I am trying to compare two variables using ksh under red hat. The error I get is: -ksh: .: MDA=`md5sum /tmp/ftp_dir_after_transfer | cut -d' ' -f1 ` MDB=`md5sum /tmp/ftp_dir_before_transfer | cut -d' ' -f1 `... (3 Replies)
Discussion started by: old_mike
3 Replies

4. Programming

linked list node with pointer to struct

Suppose to have: struct Tstudent { string name, surname; int matriculation_num; }; struct Tnode { Tstudent* student; Tnodo* next; } L;I want to deference that "student" pointer. For example, I tried with: *(L->student).matriculation_numbut it not worked, as terminal... (4 Replies)
Discussion started by: Luke Bonham
4 Replies

5. Programming

Pointer Arithmetic In C

I have a fundamental question on C pointer arithmetry.. Suppose i have a c string pointer already pointing to a valid location, Can I just do a charptr = charptr +1; to get to the next location, irregardless if my program is 32 or 64 bits? or should i do it this way: charptr =... (1 Reply)
Discussion started by: Leion
1 Replies

6. Shell Programming and Scripting

Strings to integers in an arithmetic loop

Hi all, Could someone please advise what is the correct syntax for my little script to process a table of values? The table is as follows: 0.002432 20.827656 0.006432 23.120364 0.010432 25.914184 0.014432 20.442655 0.018432 20.015243 0.022432 21.579517 0.026432 18.886874... (9 Replies)
Discussion started by: euval
9 Replies

7. UNIX for Dummies Questions & Answers

Delete strings in file1 based on the list of strings in file2

Hello guys, should be a very easy questn for you: I need to delete strings in file1 based on the list of strings in file2. like file2: word1_word2_ word3_word5_ word3_word4_ word6_word7_ file1: word1_word2_otherwords..,word3_word5_others... (7 Replies)
Discussion started by: roussine
7 Replies

8. Shell Programming and Scripting

Take a list if strings from a file and search them in a list of files and report them

I have a file 1.txt with the below contents. -----cat 1.txt----- 1234 5678 1256 1234 1247 ------------------- I have 3 more files in a folder -----ls -lrt------- A1.txt A2.txt A3.txt ------------------- The contents of those three files are similar format with different data values... (8 Replies)
Discussion started by: realspirituals
8 Replies

9. Programming

unidirectional linked list pointer problem

I am trying to test some operations on a directed list. However, the declaration of a pointer is giving me trouble. I seem to have done something incorrectly because I get an error: "listtest.c:29: warning: 'p' may be used uninitialized in this function" Can anyone help? This is my code... (6 Replies)
Discussion started by: bluetxxth
6 Replies

10. UNIX for Beginners Questions & Answers

How to pass strings from a list of strings from another file and create multiple files?

Hello Everyone , Iam a newbie to shell programming and iam reaching out if anyone can help in this :- I have two files 1) Insert.txt 2) partition_list.txt insert.txt looks like this :- insert into emp1 partition (partition_name) (a1, b2, c4, s6, d8) select a1, b2, c4, (2 Replies)
Discussion started by: nubie2linux
2 Replies
All times are GMT -4. The time now is 03:54 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy