i use this struck
public struct rec
{
public string name;
public int number;
public rec* link
}
but program gives an error for string in struct.
I don't want to use char* type declaration for string defining...
Printable View
i use this struck
public struct rec
{
public string name;
public int number;
public rec* link
}
but program gives an error for string in struct.
I don't want to use char* type declaration for string defining...
uou must use the unsafe keyword
note you cant do that with any local varaibles..if you need it for local variables, use a block unsafeCode:unsafe public rec* link
Code:
unsafe
{
//any pointer decalrations in here
}
Sorry Cander, I don't think that's it.
I've played with this is a little bit and there is a problem declaring structures with strings. Because the string field cannot be declared with fixed length, the compiler doesn't know how much space to alot for the structure in memory.
Regretfully, there is no construct like string * 80 in C#.
If someone figures out a way around this...Please let me know. Binary file I/O of existing formats is a pain without this .
he is not talking about fixed length strings..we are talking about pointers as in
int *myint;
the * in C is for pointers not fixed length variables.
I think he might want to just the char pointer, instead of the string, because a string is really an array of chars, and with a char array, you get the fixed lenght.
if I define a char pointer in struct how can I assign my string to this char pointer in the string?:confused:
Or is there any other way to assign a string to a pointer.Please explain with code:)
THANKS
Hey SelJuk, what exactly are you trying to do. I tried your code and it compiled without any error.
Just do what Cander said and put the unsafe keyword in front of all pointer declarations. Also you want to set the unsafe property to true in the project properties.
BTW I'm currently having problems trying to get a char to work in a struct since all members have to be explicity initialized inside the constructor. I might have more luck with a class.
I'll try in the morning.
Hope that helps
which code did you try
did you try this code
public struct rec
{
public string name;
rec* link;
}
and in the main block
rec* p=stackalloc rec[1];
(*p).name="selcuk";
did it accept this code
if it accept this code please send the code to me...;)
SelJuk you did not post this code in the above post
However this code worksCode:rec* p=stackalloc rec[1];
(*p).name="selcuk";
I'll try the new code and reply ASAP.Code:public struct rec
{
public string name;
rec* link;
}
Seljuk could you explain what you are trying to do?
When I run your code I'm getting "Cannot take the address or size of a variable of a managed type ('TestCSharp.Form1.rec')"
I'll still take a look at it though.
yes this is the problem that i want to solve.
I can not get a memory adress from the memory for struct which includes string variable...
DO you know the answer...
If what you are doing is really important, you might want to consider using C++. I did some research this morning. You cant use managed types with unsafe code, so string wont work.
I tried this code
This compiled, but to store more than one character at name, you have to set name to stackallocated buffer.Code:public unsafe struct rec
{
public char* name;
public rec* link;
}
eg
This is where the problem lies. If I try to do this in the struct, I get an error, therefore you have to do this in the structs constructor, and every struct member have to be initialized in the constructor if you explicity declare a constructor.Code:char* name = stackalloc char[20];
Also, structs are declared on the stack if you use the new keywork, and on the heap if you dont use the new keyword.
You might want to to try a different approach to this, since pointers are not supported fully in C#.
I'll try to figure something out though.
Another thing, could you post some more code so I can have a better understanding of what ou trying to accomplish.
Hope that helped.
Using an object reference instead of a pointer should work much better. The performance hit shouldn't be too great, especially since it's "Safe".
PHP Code:class LinkTest
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
LinkedList MyList = new LinkedList("Charley Brown", 1);
MyList.Next = new LinkedList("Peppermint Patty",2);
}
}
class LinkedList
{
public LinkedList(string Name, int Number)
{
this.Name = Name;
this.Number = Number;
}
public string Name;
public int Number;
public LinkedList Next;
}
Sorry Scott but I must use pointers in my program.
Devgrp you are true I can't use strings with pointers but our teachers said that we can declare char pointers in the struct to memorize the string.
public struct rec
{
public char* name;
public rec* link;
}
after
in the program
as you write we can declare
char* name = stackalloc char[20];
I didn't write the code that I can get a string from user and assigning it with a memory address.
Like this one..: (this like my code)
public struct rec
{
public char* name;
public rec* link;
}
public static unsafe void main()
{
string variable;
variable=console.Writeline;
rec* p=stackalloc rec[1];
**********************
in this area how can define char pointer and assign it into the struct (I haven't solve it yet)
(*(*p)).name[i] works :confused:
or (*(*p)).name[i]=variable.Substring(0,1);
**********************
}
After numerous attempts, I finally figured out a way to do this.
You wont be able to use strings, since they are managed types.
Here is the code
Code:using System;
namespace CSharpTestConsole
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
public unsafe struct rec
{
public char* name;
public rec* link;
}
[STAThread]
static unsafe void Main(string[] args)
{
rec* p = stackalloc rec[1];
char *nameBuffer = stackalloc char[20];
int i = 0;
int sz = 0;
char flag = 'a';
while(flag != '\n') //read from the console, and check for newline character
{
nameBuffer[i] = Convert.ToChar(Console.Read()); //reading into buffer
flag = nameBuffer[i];
sz++; //increment sz
i++; //increment i
}
p->name = nameBuffer; //set p struct member name to point to address of namebuffer
for(i = 0; i < sz; i++)
{
Console.Write(p->name[i]); //iterate and write out the contents of what
} //the struct member name is pointing at.
//(*p).name[i] notation can also be used for the above
Console.ReadLine();
}
}
}
Thank you very much
:)
You save my life
No problem.