LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-06-2010, 08:58 PM   #1
trist007
Senior Member
 
Registered: May 2008
Distribution: Slackware
Posts: 1,004

Rep: Reputation: 67
Is there a way to print a range of a char array without a for loop?


Program in C
Say I have a char array of 1024 bytes called buf1.

But I only want to print the chars in index 0 up to index 30. I know I could do this with a for loop. But is there any other way? What about maybe storing from 31-1024 to another char array say buf2 with strcpy and somehow popping 31+ out of the buf1 char array?

What if I wanted to print chars in index 30 to 36? Any quick way without a for loop?

Last edited by trist007; 11-06-2010 at 09:04 PM.
 
Old 11-06-2010, 09:13 PM   #2
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 42
Code:
#include <stdio.h>

int main()
{
        char a[1024] = "abcdefghijklmnopqrstuvwxyz";
        printf("Characters 0 through 2 of a[]: %.3s\n", a);
        printf("Characters 10 through 15 of a[]: %.6s\n", &a[10]);
        return 0;
}
Prints this:
Code:
Characters 0 through 2 of a[]: abc
Characters 10 through 15 of a[]: klmnop
The "%.Ns" notation in the printf string is "%s" with a precision specifier. It's buried in the geek-speak in the printf(3) man page
 
1 members found this post helpful.
Old 11-06-2010, 09:21 PM   #3
trist007
Senior Member
 
Registered: May 2008
Distribution: Slackware
Posts: 1,004

Original Poster
Rep: Reputation: 67
That's awesome, thank you.
 
Old 11-07-2010, 10:15 AM   #4
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: Debian lenny, Slackware 12
Posts: 809

Rep: Reputation: 178Reputation: 178
To demonstrate that the fancy format string saves not only code complexity but actual time, run this shell script. You may wish to play with its first line, and possibly its second line.

I got this output.
Code:
running with compilation optimization O0
format string is %.25s
loop   way is 8 seconds
format way is 4 seconds
running with compilation optimization O1
format string is %.25s
loop   way is 7 seconds
format way is 5 seconds
running with compilation optimization O2
format string is %.25s
loop   way is 8 seconds
format way is 4 seconds
running with compilation optimization O3
format string is %.25s
loop   way is 7 seconds
format way is 4 seconds
Here is the script.
Code:
export iterations=10000000
export length=25
rm -f O0 O1 O2 O3; cat > 1.c <<EOD; \
gcc -O0 -Werror -Wall 1.c -o O0; \
gcc -O1 -Werror -Wall 1.c -o O1; \
gcc -O2 -Werror -Wall 1.c -o O2; \
gcc -O3 -Werror -Wall 1.c -o O3; \
./O0 O0; \
./O1 O1; \
./O2 O2; \
./O3 O3
#include <sys/stat.h>
#include <sys/times.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

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

  long        iterations;
  long        length;
  long        single_iteration;
  long        ticks_per_second;

  char       *theoretical_string;

  char        format_string[80];

  clock_t     loop_way;
  clock_t     format_way;

  struct tms  the_beginning;
  struct tms  the_middle;
  struct tms  the_end;

  FILE       *phyle;

  phyle=fopen("/dev/null","a");

  if(phyle==NULL)
  {
    fprintf(stderr,"couldn\'t open /dev/null\n");

    exit(1);
  }

  printf("running with compilation optimization %s\n",
         argv[1]
        );

  iterations=strtol(getenv("iterations"),NULL,0);
  length    =strtol(getenv("length"    ),NULL,0);

  theoretical_string=malloc(length+2);

  if(theoretical_string==NULL)
  {
    fprintf(stderr,"string is theoretically too long\n");

    exit(1);
  }

  memset(theoretical_string,'A',length+1);
  theoretical_string[length+1]=0;

  sprintf(format_string,"%%.%lds",length);
  printf("format string is %s\n",format_string);

  loop_way  =0;
  format_way=0;

  for(single_iteration=0;
      single_iteration<iterations;
      single_iteration++
     )
  {
    times(&the_beginning);

    for(jndex=1;jndex<length+1;jndex++)
    {
      fprintf(phyle,"%c",theoretical_string[jndex]);
    }

    times(&the_middle);

    fprintf(phyle,format_string,theoretical_string+1);

    times(&the_end);

    loop_way+=(the_middle   .tms_utime+the_middle   .tms_stime)
             -(the_beginning.tms_utime+the_beginning.tms_stime);

    format_way+=(the_end   .tms_utime+the_end   .tms_stime)
               -(the_middle.tms_utime+the_middle.tms_stime);
  }

  ticks_per_second=sysconf(_SC_CLK_TCK);

  printf("loop   way is %ld seconds\n",  loop_way/ticks_per_second);
  printf("format way is %ld seconds\n",format_way/ticks_per_second);

  return 0;

} /* main() */
EOD
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I point a char pointer to a part of another char array? trist007 Programming 8 11-06-2010 08:56 PM
scanf reading newline into char array while reading 1 char at a time austinium Programming 6 09-27-2010 12:27 AM
How to convert short array to char array? bvkim Programming 4 06-08-2010 10:26 AM
C++ help Dynamic array and "invalid conversion from ‘char’ to ‘char*’" heathf Programming 2 04-25-2009 10:20 PM
search in char array xxfunkxx Programming 2 12-13-2004 12:23 AM


All times are GMT -5. The time now is 03:40 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration