473,385 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Yet another pointer related problem

The purpose of the following program is to store all the
arguments passed to main() into an array of char, and print'em out
onto the screen.

#include <stdio.h>

int main (int argc, char **argv)
{
int i;
char buffer[1024];
char *pb = buffer;

if (argc == 1)
exit (1);

for (i = 1; i < argc; ++i)
{
while (*argv[i] != '\0')
*pb++ = *argv[i]++;
*pb++ = ' ';
}
*pb = '\0';

printf ("%s\n", buffer);

return 0;
}

but when I tried to modify the string manipulation process
only using pointer arithmetic as below, I got stucked and
can't figure out why. probably I'm still got hazy about the
concept of pointer. hope you can help me out, thanx.

for(;argc>1;--argc)
{
while(*++*argv != '\0')
*pb++ = **argv++;
*pb++ = ' ';
}
Nov 14 '05 #1
7 950

"sugaray" <ru****@sohu.com> wrote in message news:ad**************************@posting.google.c om...
The purpose of the following program is to store all the
arguments passed to main() into an array of char, and print'em out
onto the screen.

#include <stdio.h>

int main (int argc, char **argv)
{
int i;
char buffer[1024];
You don't need this when you have argv with you!
char *pb = buffer;

if (argc == 1)
exit (1);

for (i = 1; i < argc; ++i)
{
while (*argv[i] != '\0')
*pb++ = *argv[i]++;
*pb++ = ' ';
}
*pb = '\0';

printf ("%s\n", buffer);

return 0;
}

but when I tried to modify the string manipulation process
only using pointer arithmetic as below, I got stucked and
can't figure out why. probably I'm still got hazy about the
concept of pointer. hope you can help me out, thanx.

for(;argc>1;--argc)
{
while(*++*argv != '\0')
*pb++ = **argv++;
*pb++ = ' ';
}


F:\Vijay\c> type argc.c
#include <stdio.h>
#include <stdlib.h>

int
main ( int argc, char *argv[] )
{
puts ( "The output:" );
while ( argc-- )
{
while ( **argv )
putchar ( *(*argv)++ );
putchar ( ' ' );
*argv++;
}
return EXIT_SUCCESS;
}

F:\Vijay\c>gcc -Wall argc.c
F:\Vijay\c>a.exe 1 2 3 vijay
The output:
f:/vijay/c/a.exe 1 2 3 vijay

--
Vijay Kumar R Zanvar
Movies - http://www.geocities.com/vijoeyz/misc/movies.html
Nov 14 '05 #2
sugaray wrote:
The purpose of the following program is to store all the arguments
passed to main() into an array of char, and print'em out onto the
screen.


#include <stdio.h>

int main(int argc, char **argv)
{
char **p;

for (p = argv; *p != NULL; ++p) puts(*p);

return 0;
}

or

#include <stdio.h>

int main(int argc, char **argv)
{
int i;

for (i=0; i < argc; ++i) puts(argv[i]);

return 0;
}

Nov 14 '05 #3
In <c2**********@news-rocq.inria.fr> Grumble <in*****@kma.eu.org> writes:
sugaray wrote:
The purpose of the following program is to store all the arguments
passed to main() into an array of char, and print'em out onto the
screen.


#include <stdio.h>

int main(int argc, char **argv)
{
char **p;

for (p = argv; *p != NULL; ++p) puts(*p);

return 0;
}

or

#include <stdio.h>

int main(int argc, char **argv)
{
int i;

for (i=0; i < argc; ++i) puts(argv[i]);

return 0;
}


Two examples of wasteful programs that can be combined into an
environmentally friendly one:

#include <stdio.h>

int main(int argc, char **argv)
{
while (*argv) puts(*argv++);
return 0;
}

Dan :-)

P.S. This example and the canonical strcpy loop are at the limit of
what I consider readable C code. People who don't understand them
cannot consider themselves C programmers and people who abhor them
are likely to never like C.
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #4
ru****@sohu.com (sugaray) wrote:
for(;argc>1;--argc)
{
while(*++*argv != '\0')


You can modify argv, because it's a function parameter. You can also
modify argv[x][y] and friends, because argv[x] point to writable memory.
But you cannot modify argv[x], because the pointers in argv themselves
are (or at least, are allowed to be) constant. From the Standard:

# The parameters argc and argv and the strings pointed to by the argv
# array shall be modifiable by the program,

and note that nothing is said about argv's members themselves being
modifiable, only about the strings they point to.

Therefore,

++*argv

is not correct.

Richard
Nov 14 '05 #5
Dan Pop wrote:
.... snip ...
Two examples of wasteful programs that can be combined into an
environmentally friendly one:

#include <stdio.h>

int main(int argc, char **argv)
{
while (*argv) puts(*argv++);
return 0;
}


My version, which reverses the display order:

#include <stdio.h>
int main(int argc, char** argv) {
while (argc--) puts(argv[argc]);
return 0;
}

Both evince the clarity of using while in place of for. These are
actually useful when suspicious of the argument passing
mechanisms, such as layers of scripts and shells.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #6
In <40***************@news.individual.net> rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
ru****@sohu.com (sugaray) wrote:
for(;argc>1;--argc)
{
while(*++*argv != '\0')


You can modify argv, because it's a function parameter. You can also
modify argv[x][y] and friends, because argv[x] point to writable memory.
But you cannot modify argv[x], because the pointers in argv themselves
are (or at least, are allowed to be) constant. From the Standard:

# The parameters argc and argv and the strings pointed to by the argv
# array shall be modifiable by the program,

and note that nothing is said about argv's members themselves being
modifiable, only about the strings they point to.

Therefore,

++*argv

is not correct.


However, even Kernighan got it wrong, when he wrote *++argv[0] in the
example at page 117. Is it mentioned in the K&R2 errata?

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #7
All supplied examples, although correct, don't
generate the originally intended output.

CBFalconer wrote:
Dan Pop wrote:

... snip ...
Two examples of wasteful programs that can be combined into an
environmentally friendly one:

#include <stdio.h>

int main(int argc, char **argv)
{
while (*argv) puts(*argv++);
return 0;
}

My version, which reverses the display order:

#include <stdio.h>
int main(int argc, char** argv) {
while (argc--) puts(argv[argc]);
return 0;
}

Both evince the clarity of using while in place of for. These are
actually useful when suspicious of the argument passing
mechanisms, such as layers of scripts and shells.


Nov 14 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: tirath | last post by:
Hi all, I have a templated class that derives from a non-templated abstract class. How do I then cast a base class pointer to a <templated> derived class pointer in a generalised fashion? ...
13
by: xuatla | last post by:
I encountered "segmentation fault" and I checked my code, found the following problem: I want to reallocate memory for an array. I defined the following function: int reallocateMemory( double...
27
by: Erik de Castro Lopo | last post by:
Hi all, The GNU C compiler allows a void pointer to be incremented and the behaviour is equivalent to incrementing a char pointer. Is this legal C99 or is this a GNU C extention? Thanks in...
49
by: elmar | last post by:
Hi Clers, If I look at my ~200000 lines of C code programmed over the past 15 years, there is one annoying thing in this smart language, which somehow reduces the 'beauty' of the source code...
3
by: Lalatendu Das | last post by:
Hi , Any way i have problem related to pointer let's say i have a double pointer like node_t **headptr =NULL ; //global one // let's say this is the defination of node_t typedef struct list {...
17
by: Christian Wittrock | last post by:
Hi, What does ANSI C say about casting an 8 bit pointer to a 16 bit one, when the byte pointer is pointing to an odd address? I have detected a problem in the Samsung CalmShine 16 compiler. This...
42
by: xdevel | last post by:
Hi, if I have: int a=100, b = 200, c = 300; int *a = {&a, &b, &c}; than say that: int **b is equal to int *a is correct????
14
by: siddhu | last post by:
Dear Experts, In the following Program class A { public: void print() { cout<<"Hello World"<<endl;
1
by: urkel | last post by:
Hi everyone, I critically need help to solve this problem related to pointer in C++ Basically, I have a C/C++ program "retardselfenerg" calling a Fortran 90 subroutine "surfGF4.f90". i am so...
3
by: ricardopf | last post by:
Hello all, I'm facing some problems to compile a new module for Linux Ubuntu 8.04, kernel version 2.6. Above are two pieces of code related to the problem. linux/netfilter.h //begin...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.