1

Let's say I have:-

char Data[1000];

After doing some data extraction and manipulation, passing it around into helper functions (as char * Data), I am doing the following to remove a leading ',' from the data:-

if (Data[0] == ',') Data++;

And this works like a charm.

However as I was building my code up, I started using struct, instead of singular variables.

So now I have this:-

struct BigData
{
   char Data[1000];
}

I still manipulate it and pass it along and everything works fine until I try to remove the leading ','. My above methodology doesn't work as follows:-

if (_bigData.Data[0] == ',') _bigData.Data++;

for obvious reasons. So I decided to create a temp char array as follows:-

char temp[1000];
strcpy(temp, _bigData.Data);
if(temp[0] == ',') temp++;

Can anyone explain to me why this doesn't work?

I am just now beginning to code in C (coming from C# where such string manipulations are a very surface level procedure).

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
Philo
  • 1,931
  • 12
  • 39
  • 77
  • 1
    You comparisons should be `==` to compare `temp[0]` and `','`. `=` is the assignment operator. Does this give you the results you expect? – Mikel Rychliski Apr 15 '16 at 18:30
  • 6
    `Data++;`? That should not compile because `Data` is an array name. – Yu Hao Apr 15 '16 at 18:30
  • Sorry that was a typo on my part, I was just writing pseudo code...well that does compile and give me the required result. here is what I found:- http://stackoverflow.com/questions/4295754/how-to-remove-first-character-from-c-string – Philo Apr 15 '16 at 18:31
  • 1
    You must define `char *temptr = temp` or whatever and manipulate that instead. – Weather Vane Apr 15 '16 at 19:12
  • 2
    `char Data[1000]; ... if (Data[0] == ',') Data++;` cannot "work like a charm". It's a syntax error. – Weather Vane Apr 15 '16 at 19:15
  • @WeatherVane - from the OP's text: "...passing it around into helper functions (as `char * Data`)" - I can only assume the line he's quoting is in said-function, not shortly after that var decl. It is the only path i see where this "works" at all. – WhozCraig Apr 15 '16 at 20:04
  • @WhozCraig yes I see. Problem with duplicated names and code fragments! – Weather Vane Apr 15 '16 at 20:07
  • @WeatherVane I suspect so as well. – WhozCraig Apr 15 '16 at 20:07
  • you might use something similar to: `if ( ',' == Data[0]) { memmove( Data, Data+1 ); }` Which has advantages. 1) `memmove()` allows overlapping copies, 2) does not require an additional stack usage 3) permanently removes any leading comma. Notice the literal ',' is on the left size so an oops like using `=` rather than `==` will be caught by the compiler. – user3629249 Apr 16 '16 at 16:53

2 Answers2

3

Incrementing a pointer is OK. Data is a pointer.

// After doing some data extraction and manipulation, 
// passing it around into helper functions (as char * Data), 
// I am doing the following to remove a leading ',' from the data:-
void helper_function(char *Data) {
  if (Data[0] == ',') Data++;
  ...

Incrementing an array is not legal C syntax. temp[] is an array.

char temp[1000];
...
if(temp[0] == ',') temp++;  // error

Perhaps OP could assign the array to a pointer and then increment the pointer to meet code's objective.

char temp[1000];
char *p = temp;
...
if(p[0] == ',') p++;  // OK

..., an expression that has type ‘‘array of type’’ is converted to an expression with type ‘‘pointer to type’’ that points to the initial element of the array object ... C11dr §6.3.2.1 3

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • Yeah, though I always prefer/recommend `*ptr = &arr[0]` in this situation as it's completely clear about what it's doing, no need to squint at it for a few seconds :) – underscore_d Apr 15 '16 at 20:24
-3

Declare temp like this:

char *temp[1000]

then try this:

int index = 0;
while(temp[index] != '\0')         //check if temp[index] is still valid
{
    if (temp[index] == ',')
    {
        index++;
    }
    else
        break;
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
testfile
  • 2,145
  • 1
  • 12
  • 31
  • ...because a lot of people, for no apparent reason, default to using postfix operators even when they don't need the pre-operator value? but anyway - `else break` lolwut, this invalidates the purpose of using a loop. also, you're declaring an array of pointers, not chars... oh, my mistake, `chat`s, whatever those are... @Philo, the postfix is the least of the problems here! – underscore_d Apr 15 '16 at 18:57
  • sure I understand. That was not very well written. I guess I could just use memmove.... but I thought the other was kinda slick. – Philo Apr 15 '16 at 19:01
  • well, `strncpy` is often better for strings as it won't copy past the null, assuming you don't care about anything thereafter in the buffer. if you do, `memcpy` is better; `memmove` is only needed if the ranges might overlap. **however**, i don't see why you need a copy at all...? just declare a pointer to the start of your buffer, increment past the comma, and pass the result to whatever manipulation/printing awaits it. that's _effectively_ what you did with the raw `char[]`, but the key is to use a real pointer, not an `arrayElement[0]` that _happens, in certain cases,_ to decay to a pointer – underscore_d Apr 15 '16 at 19:08
  • I tried doing it with a pointer as:- char *temp[1000]... then do strcpy where I get the value from the struct into the temp...and then perform the ++ manipulation.. it didn't work for me.. I am sure I am not understanding this yet – Philo Apr 15 '16 at 19:14
  • @underscore_d `strcpy` will not copy past the terminator either. What `strncpy` adds is the size of the target buffer. *That* is what it won't copy past. – Weather Vane Apr 15 '16 at 19:33
  • You know `temp[index]` is `char*`, not `char`, *right* ? The first line in this answer declares an array of one-thousand `char *` (i.e. `char` *pointers*). – WhozCraig Apr 15 '16 at 20:02
  • @WeatherVane Thanks: my C is rusty. :) Would mod up the comment but I'm all voted out for the day. I was comparing `str[n]cpy` to the `mem` functions, rather than comparing the `str` siblings. Couldn't quite remember the difference but _did_ remember the `n` version being safer, so I defaulted to recommending that. In retrospect, the reason is obvious, since that's the only way I can think of to gain safety, heh. – underscore_d Apr 15 '16 at 20:22
  • this line: `char *temp[1000]` is declaring an array of 1000 pointers to character, That is not what is needed – user3629249 Apr 17 '16 at 06:10