Click here to Skip to main content
15,884,353 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
int my_streln(char *s) // q1
{
unsidned int counter = 0; // q2
while (*s)
{ ++counter;    // q3
++s; }
return counter;


}


// this funciton should compare 2 strings whether they are the same
int my_strcmp(char *s1, char *s2) { //q4 
  for (int i = 0;; i++) {
    if (s1[i] != s2[i]) {
      return 1;
    } else if (s1[i] == '\0') {   //q5
      return 0;
    }
  }
}




my first Question (q1) why we use
char *s
as a function parameter. I know that it's a pointer but don't understand the principle of it. I watched videos but I cant get it, how does it work in that situation, and why we use it and don't use the simple string function parameter.
i know that pointer opints to the adress ot another variable but in that Case we do'nt point to any adress because we dont use the &sign

my second question: why we use unsigned keyword
third Question: why we use ++counter and not ++counter
.........
q4: the same why we use char *s1 ..as i know the pointer should point to an adress..in that way
char * s1 s1 = &something


...
q5: why do we ompare
s1[i] == '\0'
and to
s2[i]


What I have tried:

...........................................................
Posted
Updated 19-May-21 5:49am

q1: The variable s is a pointer to an array of characters, so you need to use the pointer type in the parameter field of the function.

q2. That is just a matter of choice, it is not essential to use unsigned.

q3. In this case it does not matter, ++counter or counter++ both do what is needed. You only need to be specific when you are capturing the value at the time that you use the increment operator.

q4. If you wish to use s1 as a pointer to something, then you must declare it as such:
C++
char c1 = 'l'; // c1 is a single character value
char * s1 = "a string"; // s1 is a pointer to an array of characters


q5. The final character in a C character array is '\0' (i.e a zero byte) so that is the way to test if you have reached the end of the string.

See Organization of the C Language Reference | Microsoft Docs[^] for full details on the C language.

[edit]
When creating a 'string' in C you have two possibilities. but remember that there is no string type in C, they are just simple arrays. When you create a string it can be a local array or a pointer to literal:
C++
char s1[] = "this is a local array";
char *s2 = "this is a literal"; 

Variable s1 allocates space for the characters in the normal data space of the application. Variable s2 creates a pointer in the normal data space, but creates the string literal separately. When accessing the data you may use either pointers, or array offsets. Although s1 is an array, its name is treated by the compiler as a pointer to the first character. So when calling the following function to do something with the data you can do :
C++
void function(char *pszText)
{
    while (*s1)
    {
        // do something with each character
    }
}

// and you can call it with:
function(s1); // passes an auto-generated pointer to the array
// or
function(s2); // passes the predefined pointer
// or for some people
function(&s1[0]); // passes the address of the first element

[/edit]
 
Share this answer
 
v5
Comments
Ahmad Qassym 19-May-21 10:50am    
thanx..but as i know about pointers we say int *s; s = &w; that means that s is pointer to w .. why we dont use the same here ?
k5054 19-May-21 15:09pm    
When you have
char *s = "Hello"
the string compiler effectively uses
char *s = (char []){ 'H', 'e', 'l', 'l', 'o', '\0'}
Writing the string in quotation marks is really just a shorthand for the fully decorated array of characters. Knowing that, you can, if you so desire, declare a int pointer (or double, float, etc) exactly the same way, e.g.
int *x = (int []){ 0, 1, 2, 3 };
Richard MacCutchan 19-May-21 16:10pm    
I was trying to keep the explanation as simple as I could.
Q1. See Richard's comment. Sometimes char[] s is used to indicate that the parameter is an array, but it'll still work the same way. When an array is passed as an argument in C/C++, it turns into ("decays" to) a pointer to the first element. But this is only true when defining a parameter: in other cases, char* and char[] are not the same. See this article[^].

Q2. unsigned should be used when it makes no sense for a value to be less than zero. The length of a string is a good example. In fact, the type size_t is commonly used to indicate that something is being counted. Depending on whether your code is targeted for a 32-bit or 64-bit CPU, size_t is equivalent to one of the C++ types uint32_t or uint64_t.

Q3. As Richard said, counter++ and ++counter do the same thing. Some people prefer the second one because it's theoretically more efficient, but it won't matter with most compilers. The only thing to watch out for is that the second one increments the value before it is used. So after i = 0, s[i++] reads the value of i[0], but s[++i] reads the value of s[1]. In either case, i is 1 afterwards.
 
Share this answer
 
v2
Comments
Ahmad Qassym 19-May-21 12:02pm    
thats what I mean , why we use char *s and not char[] s? what is the difference? when to use this and when we do use this? and what do u mean with "decays"? ..please explain it to me with simple words ..
Greg Utas 19-May-21 12:15pm    
Historically, I believe char* was the only thing available. Later, char[] (rather than a fixed size, like char[10]) was added. The article explains it at least as well as I could, so please read it. "Decays" means that when you pass an array as an argument, it gets passed as a pointer to the first element. Passing the entire array would be very inefficient, and passing a pointer also allows arrays of different sizes to be passed. When a function handles arrays of different sizes, the size is passed as a second parameter. This is unnecessary for a string, however, because the '\0' (NUL) character indicates where it ends.
Richard MacCutchan 19-May-21 12:39pm    
char* s = "foo"; and char s[] = "foo"; were both valid declarations in 1978, when my copy of K&R was published.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900