When i want to use a list of strings
i never understand it well.
Here is an easy example. It took me long to figure it out, searching the web didn't help much:
1) I don't understand why the size of len is computed that way.
2) If i loop with while over list instead of pl, i get a error complaining an lvalue would be required.
3) I would prefer to use pointer arithmetic, but i never get it sorted. If i use the index i instead it works
Thanks for explanations or a link where it is explained with examples.
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:
When compiled as a 64-bit application and run on OS X this produces:
These 2 Users Gave Thanks to Don Cragun For This Post:
Thanks, that makes it clear, especially all the comments. Main problem seems to have been to set the last element to NULL.
Divide sizeof the list by sizeof the first element i can remember. It is still a bit unclear, but i guess i confuse the strlen of the first element with sizeof (if i change the strlen of an element and then use sizeof, i always get the same size, 4 in my case).
I don't understand why i get the error: lvalue required if i increment list itself.
The both slightly unclear questions are minor problems for me (only to fully understand it, out of interest). I guess and hope it will be more clear when i understand more about C.
Thanks, that makes it clear, especially all the comments. Main problem seems to have been to set the last element to NULL.
Divide sizeof the list by sizeof the first element i can remember. It is still a bit unclear, but i guess i confuse the strlen of the first element with sizeof (if i change the strlen of an element and then use sizeof, i always get the same size, 4 in my case).
I don't understand why i get the error: lvalue required if i increment list itself.
The both slightly unclear questions are minor problems for me (only to fully understand it, out of interest). I guess and hope it will be more clear when i understand more about C.
Thanks again.
The size of a pointer to a character is a constant in any particular program; whether that pointer points to a single character or to a nul-byte terminated string of 100 characters does not affect the size of the pointer.
In your program, list is the address where an array of pointers to characters is stored. The statement:
would be an attempt to move that array to a new location in memory. Since you can't do that, the C compiler won't allow it.
You could allocate space for an array of pointers (such as by using malloc() or mmap()), set up a pointer to a pointer that points to the start of that newly allocated space, copy pointers into that space and increment that pointer to a pointer. You could allocate more space for a copy of that array of pointers and copy the array to the new space, etc., but that is a different lesson.
This User Gave Thanks to Don Cragun For This Post:
Ok, thanks a lot. You explained it very good. I ran into the problem a couple of times, but nothing i found really made it clear. I think i got it now.
No, any additional info is very welcome. Both, pointers and the whole subject of memory, is very hard for me, in general.
Reading the FAQ about name[3] and *name i think this: I am quite sure that i read somewhere (K&R?) that the compiler immediatly translates the one to the other ( i think indexed to pointers). All i can find in my book is this: "There is one difference between and array name and a pointer that must be kept in mind. A pointer is a variable, pa=a and pa++ are legal. But an array name is not a variable; constructions like a=pa and a++ are illegal"
I am not sure, but i think what i wrote above, that list++ gives me an lvalue error, is related? After Don's explanation: In your program, list is the address where an array of pointers to characters is stored. The statement: list++ would be an attempt to move that array to a new location in memory. Since you can't do that, the C compiler won't allow it."
i seem to be sure, but it won't hurt to ask if i am right.
side note: I am never sure about the right terminology. Please correct me if i use terms wrong.
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)