Re: problem with function

From: Jack Klein (jackklein_at_spamcop.net)
Date: 10/23/04


Date: Sat, 23 Oct 2004 13:58:31 -0500

On Sat, 23 Oct 2004 17:35:17 +0100, "Colin Girling"
<colin@colingirling.my-bulldog.com> wrote in
alt.comp.lang.learn.c-c++:

> Do you know what you have done?

More importantly, do you know what you have done? You have
top-posted, that is added your new material at the top with the quoted
post below. This makes technical discussions very hard to follow, and
is considered rude behavior in many technical discussion groups,
including this one.

When you post a response to another post, material you add belongs
after the quoted material that you are commenting on, as I have done
below. If you prefer to use Outlook Express for a newsreader, you can
either manually move to the bottom and add your material, or Google
for one of the patches that makes it work properly by default.

Or you can find another newsreader that doesn't ignore proper Internet
conventions, several good ones are free, including Free Agent and
Gravity.

> Firstly, newName = oldName only assigns the address of oldName to newName
> (does not copy the string).
> If you intended copying the string, you need to use the strcpy function.
> i.e.
> strcpy(newName, oldName);
>
> Also, you should pass a parameter into the function giving the string
> length.
> i.e. void changeFileName(char *oldName, char *newName, int iStrLen)
> Then you can check that you do not change memory past the end of the string.
> i.e. if( x+8 < iStrLen )
>
> Also, instead of assigning the chars to the end in the way you have, use the
> strcat function
> i.e.
> strcat(newName, ".sorted");
>
> Also, you could then test length like this:
> if( iStrLen < strlen(oldName) + strlen(".sorted") )
> strcat(newName, ".sorted");

This one is wrong, since strlen() does not count the terminating '\0'
in either character string. To have enough to concatenate two strings
you must have at least strlen(string) + strlen(string2) + 1 bytes.

> also, if you do this, set ".sorted" to a const char *.
> i.e.
> const char *pcszAppend = ".sorted";
> then
> strcat(newName, pcszAppend);
>
> Also, I suggest researching hungarian notation for variable names. Once you

Oh, no, certainly don't do that.

> get to grips with this, much easier to know what variable types are, and
> most of the industry using this also.

Most of the industry has realized that this is nonsense, and abandoned
it completely. It is useless noise that tends to hide the actual
meaning of variable names, assuming that variables are given
meaningful names to begin with. Many users of 'Hungarian' notation
don't. Perhaps they are too tired after typing all the prefix
characters.

My understanding is that even Microsoft has abandoned this now.

> Hungarian notation is where you prefix a variable name with a series of
> letters describing the data type.
>
> e.g.
> psz = point to char*

While I'm no expert on 'Hungarian', I am pretty sure you've several of
your examples wrong.

Isn't 'psz' a pointer a an array of characters containing a string?
'point to char*' would be a pointer to a pointer to char, not a
pointer to char.

> pcsz = point to const char *

Likewise here, I don't think 'pcsz' is proper 'Hungarian' for constant
pointer to a non constant pointer to characters.

> i = int
> l = long
> ul = unsigned long
> n = number (short, int or long, signed or unsigned)
> by = unsigned char
> c = char
> d = double
> etc...

You have completely missed the actual cause of the OP's problem. See
my response to the original post.

-- 
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html


Relevant Pages

  • Re: split a string
    ... But this reads a string into a single char ... Check fgetsfrom the Standard C Library as a way to get string input ... pointer to a pointer to a char. ... To handle arrays remotely. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: copy a string into a 2d array of chars
    ... This split function should allocate a 2D array of chars ... >focus the program the string is not actually split. ... later) is an array of char containing the original contents of the ... The i-th pointer will contain the starting address of the ...
    (comp.lang.c)
  • Re: problem with function
    ... Yes I did miss type the psz example, it is a pointer to char (not pointer to ... companies using Hungarian, and when I looked into one standard, I found ... >> If you intended copying the string, you need to use the strcpy function. ... > Isn't 'psz' a pointer a an array of characters containing a string? ...
    (alt.comp.lang.learn.c-cpp)
  • Re: type of "string"
    ... Well, the above writing doesn't have anything named "String", so I can't ... The function foo is declared as taking pointer to char. ... That is why compiler gives you a warning. ...
    (comp.lang.c.moderated)
  • Re: Need to know the size of the memory block pointed to by a char*
    ... Or, use structs with both the size and the pointer, rather than ... just char* values. ... strlenwill not do the job since sometimes the string is not ... void a(int length, char *ptr) ... ...
    (comp.lang.c)