473,480 Members | 5,041 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

I'm trying to increment a char** and I am unable to understand the dynamic of it

1 New Member
I have the following char** that I'm trying to get data about:
Expand|Select|Wrap|Line Numbers
  1. int print_word_count(char **argv) {
  2.   int count = 0;
  3.   char **a = argv;
  4.   while(a++){
  5.    printf("%s\n",*a);
  6.     ++count;
  7.   }
  8.   printf("The sentence contains %d %s.\n", count, words(count));
  9.   return count
using gdb I get a seg fault on the while loop. trying to incure about '*a' after incrementing shows it has address 0x00
couldn't find explanation to it on google
Nov 13 '12 #1
3 1507
vikramjit
2 New Member
a and *a even after legal limit may not contain null hence after the actual content is read *a points to firbidden location as a result on 'printf' you get an error.

You should know prior to calling 'print_word_count' how many words you have

just for your information..look at the declaration of main that accepts command line args and try to figure out why first argument is the count of arguments.
Good luck.
Nov 13 '12 #2
donbock
2,426 Recognized Expert Top Contributor
Your code assumes that argv points to an array of char pointers and that the last entry in this array is NULL. Apparently the caller did not set up the array that way so your code steps past the end of the array -- triggering a seg fault.

Typically, functions are passed both a pointer to the start of an array and the number of entries in the array. Is the argv argument to print_word_count related to the argv argument to main? If so, you should note that argc is another argument to main.
Nov 14 '12 #3
donbock
2,426 Recognized Expert Top Contributor
Line 4 tests the value of a. If it is nonzero, then a is incremented (to point to the next entry in the array) and the loop body is executed. This means that you only test the value of a after you dereference it.
Nov 14 '12 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

19
13263
by: Nancy | last post by:
Hi, When I start my PC(winXP Pro), it always says: Unknown(): Unable toload dynamic library './php_msql.dll' - The specified module could not be found. Then my Apache servre starts, load php...
3
4524
by: Jenny | last post by:
I have a procedure that take several paramters and depending of what values is submitted or not, the procedures shall return different number of rows. But to simplyfy this my example use just one...
5
3649
by: Johnathan Doe | last post by:
Why is is necessary to write a function that allocates memory for, or changes, a pointer to a char array using char** instead of char*? E.g. void modify_string( char** string ) { if( string...
16
21948
by: Mansi | last post by:
Given the following declaration: char letter = 'A'; Is there a way that I can increment letter so that 'B' is returned? Thanks. Mansi
5
996
by: Jim Bancroft | last post by:
I saw an article (kicks self for not bookmarking) on the web about those sites that ask you to type in the text shown in a dynamically-generated gif or bitmap, to confirm your registration. It's...
10
26551
by: sposes | last post by:
Im very much a newbie but perhaps somehone can help me. Ive been searching for a way to convert a std::string to a unsigned char* The situation is I have a function that wants a unsigned char*...
11
3501
by: Eric | last post by:
Is there a simple, standard statement that will reinterpret an int in the range of 0-9 as a char? I understand it's simple enough to write in a function, but I wonder if there's a more general...
1
6545
by: caiaphas | last post by:
hi, Im trying to make a trigger that monitors the deletes and updates on a table. I will use this design for many tables so Im trying to make it as dynamic as posible. Im trying to build a...
4
3587
by: =?Utf-8?B?TWlrZSBDb2xsaW5z?= | last post by:
I am trying to set up a dynamic search using linq. I believe the syntax is correct, but cannot confirm it because when I try to cast my Session from a List<to IQueryable<>, I get a cast error...
13
10693
by: tommymo | last post by:
Hi everyone I'm new to this site and the world of ASP.Net C# programming. I have been learning controls and integrating them with a SQL database. So far I have been able to move along and understand...
0
7040
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7041
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7080
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
6908
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5331
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4772
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
2994
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2980
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1299
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.